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"

?

+3


source to share


3 answers


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")

      

+3


source


I don't know if you mean this, but this works:

some_binary = a = b"\x94my quote\x94"
result = some_binary.decode()

      



And you got the result ... If you don't know which encoding to choose, you can use chardet.detect

:

import chardet
chardet.detect(some_binary)

      

+2


source


Have you tried it like this? I think you need to call decode

as a class method byte

and pass utf-8

as an argument. Add b

before the line.

string = b"\x94my quote\x94"
decoded_str = string.decode('utf-8', 'ignore')
print(decoded_str)

      

+1


source







All Articles