python csv error: line contains null byte [How to Solve]

Problems inherent in Python’s CSV package

Practice has proved that when using CSV to read a file, once the file contains a string of ‘\0’ or ‘\X00’, an error will be reported and ‘line contains null byte’ will be displayed.

Part of the reason is that there is such a string in the file itself. Another possible reason is that the CSV file is converted from excel file. The simple way to deal with it is to save it as CSV again.

If you do not want to modify the file, you need to extract these possible null bytes. The following codes:

        with open(path, 'r', encoding="UTF8") as f:
            reader = csv.reader((line.replace('\0', '') for line in f), delimiter=",")
            for row in reader:

Similar Posts: