Issue 1599055: csv module does not handle '\x00' (original) (raw)

Created on 2006-11-19 01:47 by shday, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (6)
msg30584 - (view) Author: Stephen Day (shday) Date: 2006-11-19 01:47
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 >>>
msg30585 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-11-19 15:09
Why do you think a CSV reader should support null bytes? CSV is a text format, not a binary one.
msg30586 - (view) Author: Stephen Day (shday) Date: 2006-11-19 17:08
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?
msg30587 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2006-11-19 17:23
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.
msg30588 - (view) Author: Skip Montanaro (skip.montanaro) * (Python triager) Date: 2006-11-19 20:17
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.
msg30589 - (view) Author: j_70 (j_70) Date: 2006-11-30 21:14
Could someone post a try / except block that handles this.
History
Date User Action Args
2022-04-11 14:56:21 admin set github: 44250
2006-11-19 01:47:13 shday create