473,512 Members | 14,457 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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():
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
Jul 18 '05 #1
2 3355
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" <tk****@hotmail.com> wrote in message
news:b4**************************@posting.google.c 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

Jul 18 '05 #2
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

Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
5001
by: Paul Watson | last post by:
How can I get the escapes from a command line parameter interpreted? The user provides a string on the command line. The string might contain traditional escapes such as \t, \n, etc. It might...
6
1667
by: kartik | last post by:
Escape sequences don't seem to work in strings within list comprehensions: >>> print ] What am I missing? Thank you.
8
3424
by: Joe | last post by:
I'm using Python 2.4 on Windows XP SP2. I'm trying to receive a command line argument that is a newline (\n) Here is the command line to use sample.py "\n" Here is a sample.py script
18
7149
by: Steve Litvack | last post by:
Hello, I have built an XMLDocument object instance and I get the following string when I examine the InnerXml property: <?xml version=\"1.0\"?><ROOT><UserData UserID=\"2282\"><Tag1...
3
5709
by: Ken | last post by:
HI: I'm reading a string that will be displayed in a MessageBox from a resource file. The string in the resource file contains escape sequences so they will be broken up into multiple lines. ...
5
3090
by: nummertolv | last post by:
Hi, My application is receiving strings, representing windows paths, from an external source. When using these paths, by for instance printing them using str() (print path), the backslashes are...
4
4262
by: JJ | last post by:
Is there a way of checking that a line with escape sequences in it, has no strings in it (apart from the escape sequences)? i.e. a line with \n\t\t\t\t\t\t\t\r\n would have no string in it a...
3
6487
by: slomo | last post by:
How to read strings cantaining escape character from a file and use it as escape sequences? for example, a file 'unicodes.txt' has contents: \u0050\u0079\u0074\u0068\u006f\u006e Now, ...
10
2588
by: hanaa | last post by:
Hello there. $str="This is \na ball"; echo $str; Is there a way i can make the text to be as is, without expanding the escape sequences. I know that single quoted strings do not expand escape...
0
7254
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7153
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7373
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7432
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7519
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5677
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1585
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.