backslash plague

Russell Blau russblau at hotmail.com
Fri Oct 22 16:01:24 EDT 2004


"Luis P. Mendes" <luisXX_lupe2XX at netvisaoXX.pt> wrote in message
news:2tt4b2F24cffeU1 at uni-berlin.de...
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
>
> hi,
>
> I've already read many pages on this but I'm not able to separate the
> string 'R0\1.2646\1.2649\D' in four elements, using the \ as the
separator.
>
> a='R0\1.2644\1.2344\D'

Here's your first problem: the name "a" is not mapped to the same string you
think it is.  Python interprets the backslashes in a quoted string as escape
sequences, unless you specify a raw string by putting an "r" before the
first quotation mark.  Specifically, it interprets the sequence \1 as the
character with an ASCII value of 1.  So what you've done is:

>>> a = 'R0\1.2646\1.2649\D'
>>> print a
R0?.2646?.2649\D

(The preceding line contains non-ASCII characters that may display oddly...)

I think what you really want is:

>>> a = r'R0\1.2646\1.2649\D'
>>> print a
R0\1.2646\1.2649\D

Once you have stored your string properly, what's wrong with this?

>>> print a.split("\\")
['R0', '1.2646', '1.2649', 'D']

-- 
I don't actually read my hotmail account, but you can replace hotmail with
excite if you really want to reach me.





More information about the Python-list mailing list