0

How can I remove NULL bytes using DictReader method? The following code produce error as Error: line contains NULL byte

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) 
Praveen
  • 64
  • 9

1 Answers1

1

You can replace your NULL bytes by an empty string. Like this:

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

Example:

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) 
Eduardo Soares
  • 954
  • 3
  • 13
  • If there are NULL bytes in the input, it is more likely the encoding is wrong. `encoding=utf16` is more likely the solution. – Mark Tolonen Mar 09 '19 at 17:08