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) 
2
  • 1
    Edit your Question and show one line with "NULL byte". Commented Mar 9, 2019 at 15:59
  • Your encoding is likely wrong, maybe utf16? Commented Mar 9, 2019 at 17:08

1 Answer 1

2

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) 
Sign up to request clarification or add additional context in comments.

1 Comment

If there are NULL bytes in the input, it is more likely the encoding is wrong. encoding=utf16 is more likely the solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.