我正在使用Python脚本来获取csv文件的一些列。 csv文件包含下一个项目
id,created_time,ad_id,ad_name,adset_id,adset_name,campaign_id,campaign_name,form_id,form_name,travel,full_name,email,phone_number,city
我只想要下一栏
full_name,campaign_name,form_name,travel,email,phone_number,city
原因是原始文件中的列并不总是具有相同的位置,但始终具有相同的名称
我的代码有未知错误
import sys
import csv
with open('originalFile.csv', 'r') as infile, open('file2.csv', 'a') as outfile:
# output dict needs a list for new column ordering
fieldnames = ["full_name","campaign_name","form_name","travel","email","phone_number","city"]
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
# reorder the header first
writer.writeheader()
for row in csv.DictReader(infile):
if row is not None:
# writes the reordered rows to the new file
writer.writerow(row)
错误是
Traceback (most recent call last):
File "orden.py", line 11, in <module>
for row in csv.DictReader(infile):
File "/usr/lib/python2.7/csv.py", line 107, in next
self.fieldnames
File "/usr/lib/python2.7/csv.py", line 90, in fieldnames
self._fieldnames = self.reader.next()
_csv.Error: line contains NULL byte
答案 0 :(得分:0)
可以选择忽略额外字段
writer = csv.DictWriter(outfile, fieldnames=fieldnames, extrasaction='ignore')
答案 1 :(得分:0)
这真的很简单,当有空字段时会出错。我的意思是
FULL_NAME,CAMPAIGN_NAME,FORM_NAME,旅游,电子邮件,PHONE_NUMBER,城市 JHON ,,,是的,@ gmail.com,0999999,NY
我使用先前的算法进行探测以防止这种情况(在原始的.csv文件中)
{{1}}