Re: Using repr() with escape sequences

2006-02-27 Thread nummertolv
myString = bar\foo\12foobar
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.

-- 
http://mail.python.org/mailman/listinfo/python-list


Using repr() with escape sequences

2006-02-23 Thread nummertolv
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 unprintable 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\x0coo'

 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

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Using repr() with escape sequences

2006-02-23 Thread nummertolv
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\12foobar

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

bar\foo\12foobar

typing 'print myString' prints the following:

baroo
foobar

and typing print repr(myString) prints this:

'bar\x0coo\nfoobar'


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

-- 
http://mail.python.org/mailman/listinfo/python-list