473,785 Members | 2,807 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Using repr() with escape sequences

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 naturally
interpreted as escape characters.
print "d:\thedir" d: hedir

The solution is to use repr() instead of str():
print repr("d:\thedir ") 'd:\thedir'

What I have not been able to figure out is how to handle escape
sequences like \a, \b, \f, \v and \{any number} inside the paths. Using
repr() on these escape sequences either prints the hex value of the
character (if "unprintabl e" i guess) or some character ( like in the
last example below).
print repr("d:\thedir \10") 'd:\thedir\x08'
print repr("d:\thedir \foo") 'd:\thedir\x0co o'
print repr("d:\thedir \100")

'd:\thedir@'

Could someone clear this out for me and let me know how I can find the
"real" path that I am trying to receive?

/Henrik

Feb 23 '06 #1
5 3105
On Thu, 23 Feb 2006 07:32:36 -0800, nummertolv wrote:
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 naturally
interpreted as escape characters.
print "d:\thedir" d: hedir
No. What is happening here is not what you think is happening.
The solution is to use repr() instead of str():


The solution to what? What is the problem? The way the strings are
DISPLAYED is surely not the issue, is it?
print repr("d:\thedir ")

'd:\thedir'

You have created a string object: "d:\thedir"

That string object is NOT a Windows path. It contains a tab character,
just like the print statement shows -- didn't you wonder about the large
blank space in the string?

Python uses backslashes for character escapes. \t means a tab character.
When you enter "d:\thedir" you are embedding a tab between the colon and
the h.

The solutions to this problem are:

(1) Escape the backslash: "d:\\thedir "

(2) Use raw strings that don't use char escapes: r"d:\thedir"

(3) Use forward slashes, and let Windows automatically handle them:
"d:/thedir"

However, if you are receiving strings from an external source, as you say,
and reading them from a file, this should not be an issue. If you read a
file containing "d:\thedir" , and print the string you have just read, the
print statement uses repr() and you will see that the string is just
what you expect:

d:\thedir

You can also check for yourself that the string is correct by looking at
its length: nine characters.
--
Steven.

Feb 23 '06 #2
I think I might have misused the terms "escape character" and/or
"escape sequence" or been unclear in some other way because I seem to
have confused you. In any case you don't seem to be addressing my
problem.

I know that the \t in the example path is interpreted as the tab
character (that was part of the point of the example) and what the
strings are representing is irrelevant. And yes, the way the strings
are displayed is part of the issue.

So let me try to be clearer by boiling the problem down to this:

- Consider a string variable containing backslashes.
- One or more of the backslashes are followed by one of the letters
a,b,f,v or a number.

myString = "bar\foo\12foob ar"

How do I print this string so that the output is as below?

bar\foo\12fooba r

typing 'print myString' prints the following:

bar oo
foobar

and typing print repr(myString) prints this:

'bar\x0coo\nfoo bar'
Hope this makes it clearer. I guess there is a simple solution to this
but I have not been able to find it. Thanks.

/H

Feb 23 '06 #3
nummertolv enlightened us with:
myString = "bar\foo\12foob ar"
Are the interpretations of the escape characters on purpose?
How do I print this string so that the output is as below?

bar\foo\12fooba r
Why do you want to?
typing 'print myString' prints the following:

bar oo
foobar


Which is correct.

Sybren
--
The problem with the world is stupidity. Not saying there should be a
capital punishment for stupidity, but why don't we just take the
safety labels off of everything and let the problem solve itself?
Frank Zappa
Feb 23 '06 #4
nummertolv wrote:
- Consider a string variable containing backslashes.
- One or more of the backslashes are followed by one of the letters
a,b,f,v or a number.

myString = "bar\foo\12foob ar"

How do I print this string so that the output is as below?

bar\foo\12fooba r

typing 'print myString' prints the following:

bar oo
foobar

and typing print repr(myString) prints this:

'bar\x0coo\nfoo bar'


The interpretation of escape sequences happens when the Python compiler
reads the string "bar\foo\12foob ar". You'll see that when you do
something like
map (ord, "bar\foo\12foob ar") [98, 97, 114, 12, 111, 111, 10, 102, 111, 111, 98, 97, 114]
This displays the ASCII values of all the characters.

If you want to use a string literal containing backslashes, use r'' strings: myString = r'bar\foo\12foo bar'
map (ord, myString) [98, 97, 114, 92, 102, 111, 111, 92, 49, 50, 102, 111, 111, 98, 97, 114] print myString bar\foo\12fooba r print repr (myString)

'bar\\foo\\12fo obar'

If you get the strings from an external source as suggested by your
original post, then you really have no problem at all. No interpretation
of escape sequences takes place when you read a string from a file.

Daniel
Feb 23 '06 #5
myString = "bar\foo\12foob ar"
print repr(myString)

My "problem" was that I wanted to know if there is a way of printing
"unraw" strings like myString so that the escape characters are written
like a backslash and a letter or number. My understanding was that
repr() did this and it does in most cases (\n and \t for instance). In
the cases of \a,\b,\f and \v however, it prints hexadecimal numbers.
But I guess I'll just have to live with that and as you point out, it
doesn't have to be a problem anyway.

Feb 27 '06 #6

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

Similar topics

6
5020
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 also contain escaped octal or hex such as \001 or \x09. The escapes are coming into sys.argv without shell interpretation. Do I need to use the compile module to make this work? Any suggestions? ===
6
6657
by: Walter L. Preuninger II | last post by:
I need to convert escape sequences entered into my program to the actual code. For example, \r becomes 0x0d I have looked over the FAQ, and searched the web, with no results. Is there a function that can do this, or do I need to use predefined constants or a table of the values? Below is a sample program. When run with the input w\rx, I want to see the output::
7
96330
by: teachtiro | last post by:
Hi, 'C' says \ is the escape character to be used when characters are to be interpreted in an uncommon sense, e.g. \t usage in printf(), but for printing % through printf(), i have read that %% should be used. Wouldn't it have been better (from design perspective) if the same escape character had been used in this case too. Forgive me for posting without verfying things with any standard compiler, i don't have the means for now.
5
2369
by: Anton81 | last post by:
Hi all! I used escape sequences to produce colour output, but a construct like print "%8s" % str_with_escape doesn't do the right thing. I suppose the padding counts the escape characters, too. What could be a solution?
4
4282
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 line with \n\t\t\t\thello\t\t\n would hve the string 'hello' in it. In others words, is there a method of removing all escape sequences from a string? I've tried Regex.Unescape(string) but this doesn't not seem to remove the
2
3308
by: | last post by:
I mainly work on OS X, but thought I'd experiment with some Python code on XP. The problem is I can't seem to get these things to work at all. First of all, I'd like to use Greek letters in the command prompt window, so I was going to use unicode to do this. But in the command prompt, the unicode characters are displaying as strange looking characters. I tried installing the 'Bitstream Vera Sans Mono' font in hopes it had all the...
10
2611
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 sequences. But I need to echo text that's been entered by the user in a html textarea as is. And thats why I dont want the escape sequences to be expanded. Thanks, Hanaa
5
5334
by: John Ztwin | last post by:
Hello, I have a file that contains ordinary text and some special charaters in Unicode escape sequences (\uxxxx). When I read the file using e.g. StreamReader Unicode escape sequences are not converted to their character representation. They are shown excatly same way than in file. Literals in C# code's variables are shown corretly. Can anyone tell how to read Unicode escape sequences from file so that they
10
1572
by: David C. Ullrich | last post by:
I've been saving data in a file with one line per field. Now some of the fields may become multi-line strings... I was about to start escaping and unescaping linefeeds by hand, when I realized that repr() and eval() should do. Hence the question: If s is a string, is repr(s) guaranteed not to contain line breaks? -- David C. Ullrich
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10329
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4053
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 we have to send another system
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2880
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.