How to replace a double backslash with a single backslash in python? How to replace a double backslash with a single backslash in python? python python

How to replace a double backslash with a single backslash in python?


You can try codecs.escape_decode, this should decode the escape sequences.


I'm not getting the behaviour you describe:

>>> x = "\\\\\\\\">>> print x\\\\>>> y = x.replace('\\\\', '\\')>>> print y\\

When you see '\\\\' in your output, you're seeing twice as many slashes as there are in the string because each on is escaped. The code you wrote should work fine. Trying printing out the actual values, instead of only looking at how the REPL displays them.


Python3:

>>> b'\\u201c'.decode('unicode_escape')'“'

or

>>> '\\u201c'.encode().decode('unicode_escape')'“'