Tag Archives: An error to solve

Error: line contains null byte

A line of content read by CSV contains a null byte
which in Python is \x00

In [1]: a = '\0'
In [2]: a
Out[2]: '\x00'
In [3]: b = '\x00'
In [4]: b
Out[4]: '\x00'
In [5]: a == b
Out[5]: True

Make an error file like this, print it out in PyCharm, and then copy the printed result to a CSV file

print('1,\x00,2')

OK, the exception file has the

test code

with open('file.csv', 'r') as f:
    reader = csv.reader(f)
    next(reader)

Error: line contains NULL byte
. So, how do you do that
Modify the code

with open('file.csv', 'r') as f:
    reader = csv.reader(_.replace('\x00', '') for _ in f)
    next(reader)

Why this terrible mistake?
1. The data in it has this thing
2. The file is an excel file, but the result is saved as CSV
3. Multiple processes read and write to the same file