Praveen
Praveen

Reputation: 74

list contains NULL byte, CSV DictReader

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) 

Upvotes: 0

Views: 519

Answers (1)

Eduardo Soares
Eduardo Soares

Reputation: 1000

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) 

Upvotes: 2

Related Questions