I was looking at this question: python3 replacing double backslash with single backslash [duplicate]
and sifting through the responses to similar questions: Python Replace \ with \ , Why can't Python's raw string literals end with a single backslash? , How do I unescape a unicode escaped string in python?
When I realised that none of the answers really solve this problem. Say I have a broken unicode string, it contains both escaped backslashes and escape characters:
my_str = '\\xa5\\xc0\\xe6aK\xf9\\x80\\xb1\\xc8*\x01\x12$\\xfbp\x1e(4\\xd6{;Z'
When I print it, some characters evaluate:
print(my_str)
\xa5\xc0\xe6aKù\x80\xb1\xc8*☺↕$\xfbp▲(4\xd6{;Z
I can manually fix it like this:
my_str = repr(my_str)
my_str
"'\\\\xa5\\\\xc0\\\\xe6aKù\\\\x80\\\\xb1\\\\xc8*\\x01\\x12$\\\\xfbp\\x1e(4\\\\xd6{;Z'"
my_str = my_str.replace('\\\\','\\')
print(my_str)
'\xa5\xc0\xe6aKù\x80\xb1\xc8*\x01\x12$\xfbp\x1e(4\xd6{;Z'
But at this point I have to manually copy and paste the result of print into a variable to finish the fix:
my_str = '\xa5\xc0\xe6aKù\x80\xb1\xc8*\x01\x12$\xfbp\x1e(4\xd6{;Z'
print(my_str)
¥ÀæaKù±È*☺↕$ûp▲(4Ö{;Z
How do I do this without copying and pasting?