escape character / csv module

V

V N

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?
 
A

anon

V said:
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"
 
M

MRAB

V said:
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! :)
 
J

John Machin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top