我有一个franco-france文件,我希望以年为关键,而其他信息为值。
文件的一部分看起来像这样
Year,Cyclist,Time,Distance,Hours,Minutes,Seconds,Team,CyclistNumber,TotalSeconds,Gap,Stages
1903,MAURICE GARIN,94h 33' 14'',2428,94,33,14,TDF 1903 ***,1,340394,-,6
1903,LUCIEN POTHIER,97h 32' 35'',2428,97,32,35,TDF 1903 ***,37,351155,+ 02h 59' 21'',6
1903,FERNAND AUGEREAU,99h 02' 38'',2428,99,2,38,TDF 1903 ***,39,356558,+ 04h 29' 24'',6
1903,RODOLPHE MULLER,99h 12' 44'',2428,99,12,44,TDF 1903 ***,33,357164,+ 04h 39' 30'',6
1903,JEAN-BAPTISTE FISCHER,99h 41' 58'',2428,99,41,58,TDF 1903 ***,12,358918,+ 05h 08' 44'',6
1903,ARSENE MILLOCHEAU,155h 30' 44'',2428,155,30,44,TDF 1903 ***,67,559844,+ 60h 57' 30'',6
我做了以下字典
with open("tour-de-france.csv") as file:
reader = csv.reader(file)
no_header = next(reader, None)
new_dct = {}
for x in reader:
new_dct[x[0]] = x[1:]
print(new_dct)
部分输出如下所示:
{'1903': ['ARSENE MILLOCHEAU', "155h 30' 44''", '2428', '155', '30', '44', 'TDF 1903 ***', '67', '559844', "+ 60h 57' 30''", '6']
输出仅具有1903年的最后一个人作为值,但是是否也可以使1903年的每个人作为值?
您可以使用列表列表作为值,即
with open("tour-de-france.csv") as file:
reader = csv.reader(file)
no_header = next(reader, None)
new_dct = {}
for x in reader:
new_dict.setdefault(x[0], [])
new_dct[x[0]].append(x[1:])
print(new_dct)
这将导致这样的事情
{'1903': [['MAURICE GARIN', ...], ['LUCIEN POTHIER', ...], ['FERNAND AUGEREAU', ...], ['RODOLPHE MULLER', ...], ['JEAN-BAPTISTE FISCHER', ...] ['ARSENE MILLOCHEAU', "155h 30' 44''", '2428', '155', '30', '44', 'TDF 1903 ***', '67', '559844', "+ 60h 57' 30''", '6']]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句