close

[Solved] Python CSV error: line contains NULL byte

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Python CSV error: line contains NULL byte in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Python CSV error: line contains NULL byte Error Occurs?

Today I get the following error Python CSV error: line contains NULL byte in python.

How To Solve Python CSV error: line contains NULL byte Error ?

  1. How To Solve Python CSV error: line contains NULL byte Error ?

    To Solve Python CSV error: line contains NULL byte Error Reading it as UTF-16 was also my problem.
    Here's my code that ended up working:

  2. Python CSV error: line contains NULL byte

    To Solve Python CSV error: line contains NULL byte Error Reading it as UTF-16 was also my problem.
    Here's my code that ended up working:

Solution 1


You could just inline a generator to filter out the null values if you want to pretend they don’t exist. Of course this is assuming the null bytes are not really part of the encoding and really are some kind of erroneous artifact or bug.

with open(filepath, "rb") as f:
    reader = csv.reader( (line.replace('\0','') for line in f) )

    try:
        for row in reader:
            print 'Row read successfully!', row
    except csv.Error, e:
        sys.exit('file %s, line %d: %s' % (filename, reader.line_num, e))

Solution 2

Reading it as UTF-16 was also my problem.

Here’s my code that ended up working:

f=codecs.open(location,"rb","utf-16")
csvread=csv.reader(f,delimiter='\t')
csvread.next()
for row in csvread:
    print row

Where location is the directory of your csv file.

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read