The following from an interactive session failed instead of just returning the '\x00': >>> r = csv.reader(['a,line,with,a,null,\x00,byte']) >>> r.next() Traceback (most recent call last): File "<pyshell#31>", line 1, in -toplevel- r.next() Error: string with NUL bytes >>>
I've been using the csv module to parse data coming from a serial port of a lab instrument. Occasionally the instrument transmits a lone /x00 (maybe when the power cycles, I'm not sure). Anyhow, I'm now using a try-except statement to catch the error. I guess my answer to your question is that I think the module should handle whatever string you pass it. Is there a specific reason not to handle /x00?
The core of the csv module is coded in C, so null bytes are as always notoriously problematic when handling strings. (This is why there's a specific error message only for null bytes.) I think that the try-except is the sanest solution to this.
If the device is transmitting the occasional spurious you might try wrapping your file object in a class which simply deletes any NUL bytes it encounters.