【发布时间】:2015-07-10 22:57:27
【问题描述】:
假设我在下面有一个 CSV 文件,其中包含一些 NFL 球员的数据。我的目标是读取文件,并创建一个字典,其中键作为位置,值作为元组中的玩家资料列表。
(姓名、年龄、身高、体重 - 不包括他们被选中的年份)
我对如何在阅读文件时正确创建字典感到困惑。到目前为止,我所拥有的是最底层的,但它是一团糟。
POSITION,NAME,DRAFTED,AGE,HEIGHT,WEIGHT
QB,Aaron,2005,31,6,225
WR,Jordy,2008,30,6,217
WR,Randall,2011,24,5,192
预期的字典:
dict = {
QB: [('Aaron', 31, 6, 225)]
WR: [('Jordy', 30, 6, 217), ('Randall', 24, 5, 192)]
}
# Year drafted not included.
我的:
def readTheFile(filename):
read_it = open(filename, 'r')
player_dict = {}
#ignore header row
readFile = read_it.readlines()[1:]
for row in readFile:
k,v = line.split()
d[int(k)] = v
return player_dict
【问题讨论】:
标签: python file dictionary key