escape character / csv module

Discussion in 'Python' started by V N, Jul 1, 2010.

  1. V N

    V N Guest

    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?
     
    V N, Jul 1, 2010
    #1
    1. Advertising

  2. V N

    anon Guest

    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"
     
    anon, Jul 1, 2010
    #2
    1. Advertising

  3. V N

    MRAB Guest

    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! :)
     
    MRAB, Jul 1, 2010
    #3
  4. V N

    John Machin Guest

    On Jul 2, 6:04 am, MRAB <> 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.
     
    John Machin, Jul 2, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff S
    Replies:
    12
    Views:
    13,868
    Jeff S
    Nov 10, 2003
  2. George Durzi

    RegisterStartupScript / Escape Character

    George Durzi, Jul 2, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    632
    George Durzi
    Jul 2, 2004
  3. simon.alexandre
    Replies:
    3
    Views:
    673
    simon.alexandre
    Jan 11, 2005
  4. jliu66
    Replies:
    0
    Views:
    513
    jliu66
    Oct 19, 2007
  5. slomo
    Replies:
    5
    Views:
    1,546
    Duncan Booth
    Dec 2, 2007
Loading...

Share This Page