From: V N on
string "\x00" has a length of 1. When I use the csv module to write
that to a file

csv_f = csv.writer(file("test.csv","wb"),delimiter="|")
csv_f.writerow(["\x00","zz"])

The output file looks like this:
|zz

Is it possible to force the writer to write that string?
From: anon on
V N wrote:
> string "\x00" has a length of 1. When I use the csv module to write
> that to a file
>
> csv_f = csv.writer(file("test.csv","wb"),delimiter="|")
> csv_f.writerow(["\x00","zz"])
>
> The output file looks like this:
> |zz
>
> Is it possible to force the writer to write that string?

This will do what you want:

"\\x00"
From: MRAB on
V N wrote:
> string "\x00" has a length of 1. When I use the csv module to write
> that to a file
>
> csv_f = csv.writer(file("test.csv","wb"),delimiter="|")
> csv_f.writerow(["\x00","zz"])
>
> The output file looks like this:
> |zz
>
> Is it possible to force the writer to write that string?

It can write "\x01" but not "\x00", and "\x00ABC" doesn't write anything
either.

The csv module imports from _csv, which suggests to me that there's code
written in C which thinks that the "\x00" is a NUL terminator, so it's a
bug, although it's very unusual to want to write characters like "\x00"
to a CSV file, and I wouldn't be surprised if this is the first time
it's been noticed! :-)
From: John Machin on
On Jul 2, 6:04 am, MRAB <pyt...(a)mrabarnett.plus.com> wrote:


> The csv module imports from _csv, which suggests to me that there's code
> written in C which thinks that the "\x00" is a NUL terminator, so it's a
> bug, although it's very unusual to want to write characters like "\x00"
> to a CSV file, and I wouldn't be surprised if this is the first time
> it's been noticed! :-)

Don't be surprised, read the documentation (http://docs.python.org/
library/csv.html#module-csv):

"""Note

This version of the csv module doesn’t support Unicode input. Also,
there are currently some issues regarding ASCII NUL characters.
Accordingly, all input should be UTF-8 or printable ASCII to be safe;
see the examples in section Examples. These restrictions will be
removed in the future."""

The NUL/printable part of the note has been there since the module was
introduced in Python 2.3.0.