gpt4 book ai didi

python csv.dictreader 不使用 data.gov csv

转载 作者:太空狗 更新时间:2023-10-30 02:34:53 24 4
gpt4 key购买 nike

使用来自 data.gov 的一些随机 CSV 数据,例如:“截至 2011 年 1 月,夏威夷退伍军人和受益人的墓地位置”http://www.data.gov/raw/4608我正在尝试用 python 解析 CSV 并处理每一行:

randomData = csv.DictReader(open('/downloads/ngl_hawaii.csv', 'rb'), delimiter=",")
for row in randomData:
print row

示例 CSV 数据:

d_first_name,d_mid_name,d_last_name,d_suffix,d_birth_date,d_death_date,section_id,row_num,site_num,cem_name,cem_addr_one,cem_addr_two,city,state,zip,cem_url,cem_phone,relationship,v_first_name,v_mid_name,v_last_name,v_suffix,branch,rank,war

Joe,"E","JoJo","","10/02/1920","03/12/2000","100-E","","3","HAWAII STATE VETERANS CEMETERY","KAMEHAMEHA HIGHWAY","","KANEOHE","HI","111444","","SXXXXX","Veteran (Self)","Joe","E","JoJo","","US ARMY","SGT","WORLD WAR II"

结果不是很漂亮(打印一行):

{'v_last_name': None, 'cem_addr_two': None, 'rank': None, 'd_suffix': None, 'city': None, 'row_num': None, 'zip': None, 'cem_phone': None, 'd_last_name': None, e, 'd_first_name': 'Joe,"E","JoJo","","10/02/1920","03/12/2000","100-E","","3","HAWAII STATE VETERANS CEMETERY","KAMEHAMEHA HIGHWAY","","KANEOHE","HI","11144 "SXXXXX","","US ARMY","SGT","WORLD WAR II"', 'war': None, 'v_mid_name': None, 'cem_url': None, 'cem_name': None, 'relationship': None, 'v_first_name': None, 'se one, 'cem_addr_one': None, 'd_birth_date': None, 'd_death_date': None}

如您所见,标题字段(csv 中的第一行)没有正确关联到每个后续行。

我做错了什么,还是 CSV 质量差?

感谢 Casey 询问我是否在另一个程序中打开了该文件。 Excel 弄乱了文件....

最佳答案

奇怪,我从你那里得到不同的输出。

数据.csv:


d_first_name,d_mid_name,d_last_name,d_suffix,d_birth_date,d_death_date,section_id,row_num,site_num,cem_name,cem_addr_one,cem_addr_two,city,state,zip,cem_url,cem_phone,relationship,v_first_name,v_mid_name,v_last_name,v_suffix,branch,rank,war
"Emil","E","Seibel","","10/02/1920","03/12/2010","139-E","","3","夏威夷州退伍军人公墓""KAMEHAMEHA HIGHWAY","","KANEOHE","HI","96744","","808-233-3630","Veteran (Self)","Emil","E","Seibel",,","美国陆军","SGT","第二次世界大战",

脚本:

for line in csv.DictReader(open('data.csv', 'rb'), delimiter=","):
print line

输出:

{'v_last_name': 'Seibel', None: [''], 'cem_addr_two': '', 'rank': 'SGT', 'd_suffix': '', 'city': 'KANEOHE', 'row_num': '', 'zip': '96744', 'cem_phone': '808-233-3630', 'd_
last_name': 'Seibel', 'd_mid_name': 'E', 'state': 'HI', 'branch': 'US ARMY', 'd_first_name': 'Emil', 'war': 'WORLD WAR II', 'v_mid_name': 'E', 'cem_url': '', 'cem_name': '
HAWAII STATE VETERANS CEMETERY', 'relationship': 'Veteran (Self)', 'v_first_name': 'Emil', 'section_id': '139-E', 'v_suffix': '', 'site_num': '3', 'cem_addr_one': 'KAMEHAM
EHA HIGHWAY', 'd_birth_date': '10/02/1920', 'd_death_date': '03/12/2010'}

csv.DictReader 应该自动从文件中的第一行获取字段名称是 fieldnames 参数被省略,as described in the docs .

输出中的 None: [''] 是由每行数据的尾随逗号引起的。

工作代码示例:

http://codepad.org/HdBhr4La

关于python csv.dictreader 不使用 data.gov csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5869776/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com