This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: csv module does not handle '\x00'
Type: Stage:
Components: Library (Lib) Versions: Python 2.5
Status: closed Resolution: wont fix
Dependencies: Superseder:
Assigned To: Nosy List: georg.brandl, j_70, shday, skip.montanaro
Priority: normal Keywords:

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'])

Traceback (most recent call last):
  File "<pyshell#31>", line 1, in -toplevel-
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.
Date User Action Args
2022-04-11 14:56:21adminsetgithub: 44250
2006-11-19 01:47:13shdaycreate