Modifying escape sequences in strings

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Thomas Philips

    Modifying escape sequences in strings

    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():[color=blue][color=green][color=darkred]
    >>> input_file=file ("test.txt")
    >>> x=input_file.re adline()[/color][/color][/color]
    [color=blue][color=green][color=darkred]
    >>> x
    >>> 1\\na\\n\\n2\\n \\n3
    >>> print x
    >>> 1\na\n\n2\n\n3[/color][/color][/color]

    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"[color=blue][color=green][color=darkred]
    >>> x.replace("\\", "\")[/color][/color][/color]

    SyntaxError: EOL while scanning single-quoted string[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    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("te st.txt","r","st ring_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

    #2
    Re: Modifying escape sequences in strings

    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" <tkpmep@hotmail .com> wrote in message
    news:b4a8ffb6.0 403020814.283e6 4d6@posting.goo gle.com...[color=blue]
    > 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():[color=green][color=darkred]
    > >>> input_file=file ("test.txt")
    > >>> x=input_file.re adline()[/color][/color]
    >[color=green][color=darkred]
    > >>> x
    > >>> 1\\na\\n\\n2\\n \\n3
    > >>> print x
    > >>> 1\na\n\n2\n\n3[/color][/color]
    >
    > 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"[color=green][color=darkred]
    > >>> x.replace("\\", "\")[/color][/color]
    >
    > SyntaxError: EOL while scanning single-quoted string[color=green][color=darkred]
    > >>>[/color][/color]
    >
    > 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("te st.txt","r","st ring_escape") as suggested by Peter
    > Otten. I'm trying to do the same thing in different ways to better
    > understand Python
    >
    > Sincerely
    > Thomas Philips[/color]


    Comment

    • Jeff Epler

      #3
      Re: Modifying escape sequences in strings

      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/no...00000000000000)
      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 misunderstandin g 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_esc ape" codec in Python 2.3:[color=blue][color=green][color=darkred]
      >>> '\\n'.decode("s tring_escape")[/color][/color][/color]
      '\n'
      This took a string containing backslash-n and returned a string
      containing a newline character.

      Jeff

      Comment

      Working...