所有内容都在标题中,我发现这段代码几乎满足了我的要求:
https://stackoverflow.com/a/21572244/5455842
import csv
with open('test.csv') as f:
a = [{k: int(v) for k, v in row.items()}
for row in csv.DictReader(f, skipinitialspace=True)]
我的CSV由5列6行组成(第一行是字段名)。
我的问题如下:
Firstname,Lastname,Birthdate,Answers(good/middle/bad),Comments Mark,Tolonen,12/10/1986,"3154,0",The first one John,Travolta,02/18/1954,"42,21",Would grease again Albert,Einstein,03/14/1879,"18,19,20",This guy is not stupid Isaac,Newton,12/25/1642,"2000,20,20", Should eat apple Alan,Turing,06/23/1912,"42,42,42",Hey what's up
下面是一个理想的例子:
[{'Birthdate': datetime.date(1986, 12, 10),
'Comments': 'The first one',
'Firstname': 'Mark',
'Lastname': 'Tolonen',
'Answers(good/middle/bad)': [3154, 0]},
{'Birthdate': datetime.date(1954, 02, 18),
'Comments': 'Would grease again',
'Firstname': 'John',
'Lastname': 'Travolta',
'Answers(good/middle/bad)': [42, 21]},
...
}]
给定示例数据和所需输出,如果使用Python 3.7或更高版本,字典顺序将与所需顺序相同:
输出:
相关问题 更多 >
编程相关推荐