从csv文件创建字典

时间:2016-04-25 10:23:43

标签: python-3.x

我的csv程序是 studentid,名字,中期,最终,总,等级 20135447,台达,47.00,37.00,65.00,DC 20144521,杰弗里,36.00,22.00,27.60,FF

我试过这段代码

with open('marks.csv')as file:
line=csv.reader(file)
mydict={rows[0]:rows[1:] for rows in line}
print(mydict)

我收到了以下回溯错误 Traceback(最近一次调用最后一次):   文件“”,第3行,in   文件“”,第3行,in IndexError:列表索引超出范围

但是我想要的输出是 {20135447:['Delta','47.00','37.00','65.00','DC'],'20144521':['Jeffrey','36.00','22.00','27.60','FF'] }

请帮帮我

1 个答案:

答案 0 :(得分:0)

import csv
mydic = dict()
with open('marks.csv') as myfile:
    line = csv.reader(myfile)
    for row in line:
        mydic[row[0]] = row[1:]