The issue came up in this question, which I’ll recapitulate in this code:
import csv
FH = open('data.csv','wb')
line1 = [97,44,98,44,99,10]
line2 = [100,44,101,44,102,10]
for n in line1 + line2:
FH.write(chr(n))
FH.write(chr(0))
FH.close()
import _csv
FH = open('data.csv')
reader = csv.reader(FH)
for line in reader:
if '\0' in line: continue
if not line: continue
try:
print line
except _csv.Error:
print 'error'
Run it:
$ python test.py
['a', 'b', 'c']
['d', 'e', 'f']
Traceback (most recent call last):
File "test.py", line 14, in <module>
for line in reader:
_csv.Error: line contains NULL byte
So, I guess the inclusion of NUL in the file causes an “uncatchable” exception.
The question is, besides sanitizing the file first, what’s the best way of dealing with this? How common are “uncatchable” exceptions?
You are not putting the “try” block at the right place to catch this exception. In other words, this exception is “catchable”, just revisit the question you have referenced.
The traceback clearly states that the problem is on the line with the “for” statement.