How to 'de-slashify' a string?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Aug 22 13:40:33 CEST 2009


On Sat, 22 Aug 2009 06:09:20 -0400, AK wrote:

>> Is pygresql quoting the backslash, or do you just think it is quoting
>> the backslashes? How do you know? E.g. if you have '\\303', what is the
>> length of that? 4 or 5?
> 
> Length is 4, and I need it to be length of 1. E.g.:
> 
>  >>> s = '\303'
>  >>> s
> '\xc3'
>  >>> x = '\\303'
>  >>> x
> '\\303'
>  >>> len(x)
> 4
>  >>> len(s)
> 1
> 
> 
> What I get from pygresql is x, what I need is s. Either by asking
> pygresql to do this or convert it afterwards. I can't do
> replace('\\303', '\303') because it can be any unicode character.


Use the 'unicode-escape' codec to decode the byte-string to Unicode.

>>> s = '\\303\\266'
>>> print s
\303\266
>>> s
'\\303\\266'
>>> len(s)
8
>>> u = s.decode('unicode-escape')
>>> print u
ö
>>> u
u'\xc3\xb6'
>>> len(u)
2



-- 
Steven



More information about the Python-list mailing list