string escaping problems with MySQLdb

BxT bxt at nospam.nospam
Tue Jan 9 11:02:23 EST 2001


Ok, this seems to solve my problem.
Note that i was not clear before: when i said that i was getting \\337 is
because the string i was getting from the database was of the kind
myString="blah\337" and i used print `myString` so i "discovered" the double
backslash.
I will dive more deeply in the manuals, but for now, thanks!
BxT

"Tim Hochberg" <tim.hochberg at ieee.org> ha scritto nel messaggio
news:7ZF66.128944$15.27235477 at news1.rdc1.az.home.com...
>
> The mysterious BxT spake thusly:
>
> > Hi, i know this is a new-bie question,
> > i'm using MySQLdb and, as i'm extracting records, i get string escaped
as
> > follows:
> > for example, instead of ß i'm getting \\337 (yes, with doppel \). When
i'm
> > printing the string i'm getting blah\337 (internally is blah\\337).
>
> The string you are getting only has a single '\', it is displayed with two
> because a "\337" is the escape code for the beta symbol. Hmmm, that's not
> very clear, I suggest you peruse the manual and or play around till this
> becomes clear. However, the difference you are seeing the string displayed
> in the interpreter and the string printed is that printing formats stuff
> using the str function, while in the interpreter repr is used.
>
> >I tried
> > to use string.replace but it's giving me the same string. Is there a way
> to
> > print blahß?
>
> I'm sure there's a better way, but:
>
> import re
> print re.sub(r"\\(\d\d\d)", lambda x : chr(int(x.group(1),8)), myString)
>
> or more clearly:
>
> escapedOctal = re.compile(r"\\(\d\d\d)")
> def matchToChr(match):
>     n = int(match.group(1), 8)
>     return chr(n)
>
> print re.sub(escapedOctal, matchToChr, myString)
>
>
> I'll let you figure out how that actually works....
>
> -tim
>
>
>
>





More information about the Python-list mailing list