How to 'de-slashify' a string?

Steven D'Aprano steve at REMOVE-THIS-cybersource.com.au
Sat Aug 22 05:18:52 EDT 2009


On Sat, 22 Aug 2009 04:20:23 -0400, AK wrote:

> Hi, if I have a string '\\303\\266', how can I convert it to '\303\266'
> in a general way?

It's not clear what you mean.

Do you mean you have a string '\\303\\266', that is:

backslash backslash three zero three backslash backslash two six six

If so, then the simplest way is:

>>> s = r'\\303\\266'  # note the raw string
>>> len(s)
10
>>> print s
\\303\\266
>>> print s.replace('\\\\', '\\')
\303\266


Another possibility:

>>> s = '\\303\\266'  # this is not a raw string
>>> len(s)
8
>>> print s
\303\266

So s is:
backslash three zero three backslash two six six

and you don't need to do any more.


> The problem I'm running into is that I'm connecting with pygresql to a
> postgres database and when I get fields that are of 'char' type, I get
> them in unicode, but when I get fields of 'byte' type, I get the text
> with quoted slashes, e.g. '\303' becomes '\\303' and so on.

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?


-- 
Steven



More information about the Python-list mailing list