我是一个菜鸟编码器,在使用 Python csv 模块解析 csv 文件时遇到问题。问题是我的输出显示该行中的字段值对于除第一个字段之外的所有字段都是“无”的。
这是我试图解析的丑陋 csv 文件中的第一行(其余行遵循相同的格式):
0,213726,NORTH FORK SLATE CREEK,CAMPGROUND,North Fork Slate Creek Campground | Idaho | Public Lands Information Center | Recreation Search, http://www.publiclands.org/explore/site.php?plicstate=ID&id=2268,NA,NA,NA,NA,(208)839-2211,"Nez Perce National Forest Operating Days: 305<br>Total Capacity: 25<br>
5 campsites at the confluence of Slate Creek and its North Fork. A number of trails form loops in the area. These are open to most traffic, including trail bikes.","From Slate Creek, go 8 miles east on Forest Road 354.",NA,http://www.publiclands.org/explore/reg_nat_forest.php?region=7&forest_name=Nez%20Perce%20National%20Forest,NA,NA,NA,45.6,-116.1,NA,N,0,1103,2058
这是我为解析 csv 文件而编写的代码(它不能正常工作!):
import csv
#READER SETTINGS
f_path = '/Users/foo'
f_handler = open(f_path, 'rU').read().replace('\n',' ')
my_fieldnames = ['col1', 'col2', 'col3', 'col4', 'col5', 'col6', 'col7',
'col8', 'col9', 'col10', 'col11', 'col12', 'col13', 'col14', 'col15',
'col16', 'col17', 'col18', 'col19', 'col20', 'col21', 'col22', 'col23',
'col24','col25']
f_reader = csv.DictReader(f_handler, fieldnames=my_fieldnames, delimiter=',', dialect=csv.excel)
#NOW I TRY TO PARSE THE CSV FILE
i = 0
for row in f_reader:
print "my first row was %s" % row
i = i + 1
if i > 0:
break
这是输出。它说除了第一个之外的所有字段都是空白的,我不知道为什么!任何建议将不胜感激。
my first row was {'col14': None, 'col15': None, 'col16': None,
'col17': None, 'col10': None, 'col11': None, 'col12': None,
'col13': None, 'col18': None, 'col19': None, 'col2': None, 'col8': None,
'col9': None, 'col6': None, 'col7': None, 'col4': None, 'col5': None,
'col3': None, 'col1': '0', 'col25': None, 'col24': None,
'col21': None, 'col20': None, 'col23': None, 'col22': None}