0

I am getting and error: line contains NUL. I think it means there's a strange character in my CSV file. But this program and import file worked on a different machine (both Macs), so I don't know if the cause is a different version of Python or how I am running it. From reading the other entries, I am thinking this line may also be the cause:

reader = csv.reader(open(filePath, 'r', encoding="utf-8-sig", errors="ignore"))

Appreciate any help / advice!

paths CWD=/Users/sternit/Downloads/Ten-code-4, CPD=/Users/sternit/Downloads/Ten-code/
Traceback (most recent call last):
  File "/Users/sternit/Downloads/Ten-code-4/Master.py", line 145, in <module>
    main()
  File "/Users/sternit/Downloads/Ten-code-4/Master.py", line 114, in main
    playerLists = loadFiles(CPD + "PlayerFiles/")
  File "/Users/sternit/Downloads/Ten-code-4/Master.py", line 50, in loadFiles
    for n, row in enumerate(reader):
_csv.Error: line contains NUL
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Neil R
  • 9
  • 1
  • 1
  • 1
    Does this answer your question? ["Line contains NULL byte" in CSV reader (Python)](https://stackoverflow.com/questions/7894856/line-contains-null-byte-in-csv-reader-python) – Ioanna Jan 25 '20 at 18:39

2 Answers2

2

this should work fine:

data_initial = open(filePath, "rb")
data = csv.reader((line.replace('\0','') for line in data_initial), delimiter=",")
Tal Avissar
  • 10,088
  • 6
  • 45
  • 70
0

If the csv module says you have a "NULL" (silly message, should be "NUL") byte in your reading file, I would suggest checking out what is in your file.

Try use rb, it might make problem go away:

reader = csv.reader(open(filePath, 'rb', encoding="utf-8-sig", errors="ignore"))

Depends on how the file generated, there might include NULL byte, so you might need to

  1. Open it in an editor, to see whether it is a reasonable CSV file, if the file too big, use nano or head in CLI.

  2. Using another library like pandas, which could be more robust.

  3. If the problem persists, you can replace all the '\x00', with empty string:

fi = open(filePath, 'rb')
data = fi.read()
fi.close()
fo = open('mynew.csv', 'wb')
fo.write(data.replace('\x00', ''))
fo.close()
wjandrea
  • 28,235
  • 9
  • 60
  • 81
Bill Chen
  • 1,699
  • 14
  • 24
  • Thanks, Bill. Trying them now! – Neil R Jan 25 '20 at 18:46
  • Best practice is to use a `with` statement for opening files, i.e. `with open(filePath, 'rb') as fi: data = fi.read()` and `with open('mynew.csv', 'wb') as fo: fo.write(data.replace('\x00', ''))` – wjandrea Jan 25 '20 at 18:56
  • @wjandrea, you are right on using `with`, but here is to open it up and save it, so I feel without using it would be fine. Thank for the suggestion. – Bill Chen Jan 26 '20 at 16:36