Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

replace '\\x' with '\x'

>>> s = '\\xca'
>>> s
'\\xca'
>>> s.replace('\\x', '\x')
  File "<stdin>", line 1
    s.replace('\\x', '\x')
                         ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \xXX escape

how to bypass the error? to print ‘Ê’ character instead of \\xca

PS: s.replace('\\xca', '\xca') is not what I want

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This looks like a fragment of a string literal. You could have python parse it, but you would also have to add surrounding quotes so that python views it as a string.

import ast
s = '\\xca'
fixed = ast.literal_eval('"' + s + '"')
print(fixed)

Output

Ê
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading