开发者

How to split a CSV row so row[0] is the name and any remaining items are a tuple?

开发者 https://www.devze.com 2023-04-04 04:29 出处:网络
I have a csv file in whi开发者_Python百科ch 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

I have a csv file in whi开发者_Python百科ch 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']


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.


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
0

精彩评论

暂无评论...
验证码 换一张
取 消