當前位置:成語大全網 - 新華字典 - Python csv文件轉字典,為什麽報錯

Python csv文件轉字典,為什麽報錯

把"list.csv"改成filename

def?csv2dict(fname):

new_dict?=?{}

with?open(fname,?'rb')?as?f:

reader?=?csv.reader(f,?delimiter=',')

fieldnames?=?next(reader)

reader?=?csv.DictReader(f,?fieldnames=fieldnames,?delimiter=',')

for?row?in?reader:

new_dict[row['name']]?=?row['movie']

return?new_dict