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:
print(row)
Similar Posts:
- Python read CSV file prompt “line contains null byte” error
- PHP regular matching H1 datagram error preg_ match(): Unknown modifier ‘h’ in
- HttpURLConnection Cannot write output after reading input.
- Error reporting and resolution of Python 3 using binascii method
- TypeError: strptime() argument 1 must be str, not bytes
- UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 0-1:
- TypeError: the JSON object must be str, not ‘bytes’
- [Solved] Python Numpy Data load error: Unicode error: unpicking a python object failed: Unicode decodeerror
- [Solved] XML Read Error: Invalid byte 1 of 1-byte UTF-8 sequence
- [Solved] org.yaml.snakeyaml.scanner.ScannerException: while scanning a simple key