Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Modifying escape sequences in strings

Reply
Thread Tools

Modifying escape sequences in strings

 
 
Thomas Philips
Guest
Posts: n/a
 
      03-02-2004
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
 
Reply With Quote
 
 
 
 
Jeff Epler
Guest
Posts: n/a
 
      03-02-2004
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 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

 
Reply With Quote
 
 
 
 
Larry Bates
Guest
Posts: n/a
 
      03-02-2004
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: om...
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to read strings cantaining escape character from a file and useit as escape sequences? slomo Python 5 12-02-2007 11:39 AM
Removing escape sequences from strings JJ ASP .Net 4 06-09-2007 03:56 PM
vhdl textio and escape sequences Olaf Petzold VHDL 1 11-28-2005 06:22 PM
processing escape sequences exactly like compiler does Harald Kirsch Java 0 11-19-2004 11:34 AM
Escape sequences and printing Kit C Programming 5 09-17-2003 02:10 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57