列表包含NULL字节,CSV DictReader

时间:2019-03-09 12:16:10

标签: python csv

如何使用DictReader方法删除NULL字节?以下代码产生错误为 错误:该行包含NULL字节

with open('excelfile.csv', 'r', encoding="ISO-8859-1") as file:

reader = csv.DictReader(file, fieldnames=('BANK','IFSC', 'BRANCH', 'ADDRESS'))
for row in reader: 
    frame = {'bank': row['BANK'], 'ifsc': row['IFSC'], 'branch': row['BRANCH'], 'address': row['ADDRESS'] } 
    framelist.append(frame) 

1 个答案:

答案 0 :(得分:0)

您可以将空字节替换为空字符串。像这样:

 reader = csv.DictReader(x.replace('\0', '') for x in file)

示例:

with open('excelfile.csv', 'r', encoding="ISO-8859-1") as file:

   reader = csv.DictReader(x.replace('\0', '') for x in file)
   for row in reader: 
     frame = {'bank': row['BANK'], 'ifsc': row['IFSC'], 'branch': row['BRANCH'], 'address': row['ADDRESS'] } 
     framelist.append(frame)