3

I have a csv file in which the first column is the name of a baseball player, and then each subsequent item in the file is a statistic. I'd like to be able to import the file so that the player name was equal to a tuple of the statistics.

Right now when I import the file using this code:

Orioles = file("Orioles.csv", "rU")
for row in Orioles:
    print row

I get something like this:

[Nick_Markakis, '.005', '.189', '.070', '.002', '.090']
[Adam_Jones, '.005', '.189', '.070', '.002', '.090']

I'd like to have the statistics be listed as floats rather than strings and be able to pull out the player name, and use it later, like this:

Nick_Markakis = ['.005', '.189', '.070', '.002', '.090']
Adam_Jones = ['.005', '.189', '.070', '.002', '.090']
Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
Burton Guster
  • 2,213
  • 8
  • 31
  • 29

2 Answers2

4

Having the player name be it's own variable is not as helpful as you might think: you can't iterate over the collection (because there isn't one), your code is fixed and fragile (you have to add/remove lines when a player name is added or removed from your file, etc.

If, however, you have them in a dictionary -- well, now you can iterate, and you can still ask for players by name.

player_data = file("Orioles.csv", "rU")
orioles = dict()
for row in player_data:
    row = row.split(',')
    orioles[row[0]] = tuple(map(float, row[1:]))

print orioles.keys()
# ['Adam_Jones', 'Nick_Markakis']
print orioles['Adam_Jones']
# (0.005, 0.189, 0.07, 0.002, 0.09)

Rather than the row.split(',') trick above, you'll probably want to use the csv module in real code.

Ethan Furman
  • 63,992
  • 20
  • 159
  • 237
2
Orioles = file("Orioles.csv", "rU")
stats = dict((row[0], map(float, row[1:])) for row in Orioles)
# In Python 2.7+: stats = {row[0]: map(float, row[1:]) for row in Orioles}

Now, you can access the stats like this:

print(sum(stats['Nick_Markakis'])) # 0.356
phihag
  • 278,196
  • 72
  • 453
  • 469
  • 1
    I would use `[float(stat) for stat in row[1:]]` instead of `map`, but this looks good. – Wilduck Sep 12 '11 at 22:24
  • 1
    @Wilduck `map` is actually faster by quite a bit (not that I'm suggesting premature optimization -- it's also quite easy to read) – Ethan Furman Sep 12 '11 at 23:45