Modifying escape sequences in strings

Discussion in 'Python' started by Thomas Philips, Mar 2, 2004.

  1. I have been playing around with reading strings with embedded escape
    sequences from files both using readline() and codecs.open() and have
    a question.I create a file "test.txt" with exactly one line:
    1\na\n\n2\n\n3

    I then open test.txt and then read it using readline():
    >>> input_file=file("test.txt")
    >>> x=input_file.readline()


    >>> x
    >>> 1\\na\\n\\n2\\n\\n3
    >>> print x
    >>> 1\na\n\n2\n\n3


    The readline has escaped the backslashes, so that they print
    correctly. I tried to replace the double backslashes with single
    backslashes to escape the "n"
    >>> x.replace("\\","\")


    SyntaxError: EOL while scanning single-quoted string
    >>>


    How can I replace the escaped backslash with a backslash? I realize
    that I can solve the problem by reading the file using
    codecs.open("test.txt","r","string_escape") as suggested by Peter
    Otten. I'm trying to do the same thing in different ways to better
    understand Python

    Sincerely
    Thomas Philips
     
    Thomas Philips, Mar 2, 2004
    #1
    1. Advertising

  2. Thomas Philips

    Jeff Epler Guest

    If you want to translate two backslashes into a single backslash,
    have to write
    x.replace("\\\\", "\\")
    the first is a string of length 2 and the second is a string of length
    1. I don't know why the tutorial doesn't cover this point explicitly
    (http://python.org/doc/current/tut/node5.html#SECTION005120000000000000000)
    but the language reference does (http://python.org/doc/current/ref/strings.html)
    .... but there are no sequences of two backslashes in the strings you
    were working with.

    However, I think that referring to "escaped backslashes" in the string
    you read shows that there's some other misunderstanding of what is going
    on. Is your final goal to turn the backslash-n sequences into actual
    newlines, or what? If this is your goal, then you should use the
    "string_escape" codec in Python 2.3:
    >>> '\\n'.decode("string_escape")

    '\n'
    This took a string containing backslash-n and returned a string
    containing a newline character.

    Jeff
     
    Jeff Epler, Mar 2, 2004
    #2
    1. Advertising

  3. Thomas Philips

    Larry Bates Guest

    The problem is that the backslash (\) has special
    meaning to Python. It means that the next character
    is escaped (has special meaning).

    Try following:

    x=x.replace("\\n","\n")

    This works for me.

    -Larry


    "Thomas Philips" <> wrote in message
    news:...
    > I have been playing around with reading strings with embedded escape
    > sequences from files both using readline() and codecs.open() and have
    > a question.I create a file "test.txt" with exactly one line:
    > 1\na\n\n2\n\n3
    >
    > I then open test.txt and then read it using readline():
    > >>> input_file=file("test.txt")
    > >>> x=input_file.readline()

    >
    > >>> x
    > >>> 1\\na\\n\\n2\\n\\n3
    > >>> print x
    > >>> 1\na\n\n2\n\n3

    >
    > The readline has escaped the backslashes, so that they print
    > correctly. I tried to replace the double backslashes with single
    > backslashes to escape the "n"
    > >>> x.replace("\\","\")

    >
    > SyntaxError: EOL while scanning single-quoted string
    > >>>

    >
    > How can I replace the escaped backslash with a backslash? I realize
    > that I can solve the problem by reading the file using
    > codecs.open("test.txt","r","string_escape") as suggested by Peter
    > Otten. I'm trying to do the same thing in different ways to better
    > understand Python
    >
    > Sincerely
    > Thomas Philips
     
    Larry Bates, Mar 2, 2004
    #3
    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. Olaf Petzold

    vhdl textio and escape sequences

    Olaf Petzold, Nov 26, 2005, in forum: VHDL
    Replies:
    1
    Views:
    3,533
    Mike Treseler
    Nov 28, 2005
  2. Harald Kirsch
    Replies:
    0
    Views:
    340
    Harald Kirsch
    Nov 19, 2004
  3. Kit

    Escape sequences and printing

    Kit, Sep 16, 2003, in forum: C Programming
    Replies:
    5
    Views:
    521
    Matt Gregory
    Sep 17, 2003
  4. JJ
    Replies:
    4
    Views:
    13,931
    Mark Rae
    Jun 9, 2007
  5. slomo
    Replies:
    5
    Views:
    1,627
    Duncan Booth
    Dec 2, 2007
Loading...

Share This Page