Python 3 decode strings
I realize this is probably a duplicate question, but I am having a hard time finding a solution.
In short, I have a string that I would like to decode:
raw = "\x94my quote\x94"
string = decode(raw)
expected from string
'"my quote"'
One final note is that I am working with Python 3, so raw
is unicode and therefore already decoded. Given what exactly do I need to do to "decode" the characters "\x94"
?
source to share
string = "\x22my quote\x22"
print(string)
You don't need to decode, Python 3
does it for you, but you need the correct double quote escape character"
If you have a different character set, it looks like you have Windows-1252, then you need to decode a byte string from that character set:
str(b"\x94my quote\x94", "windows-1252")
If your string is not a byte string you must encode it first, I found latin-1 encoding works:
string = "\x94my quote\x94"
str(string.encode("latin-1"), "windows-1252")
source to share