简体   繁体   中英

How to change a string to Unicode in Python 2?

I have a string like s1 = "\\xed\\xf3\\xb4\\x90" .

>>> x = u"\xed\xf3\xb4\x90"
>>> print x
íó´

How could I use s1 to print this?

I have tried:

s1= "\xed\xf3\xb4\x90"
print unicode(s1)

But I could not get íó´ . How could I get íó´ ?

The correct codec to be used here is 'latin1' :

>>> s1= "\xed\xf3\xb4\x90"
>>> print s1.decode('latin1')  # same as: unicode(s1, 'latin1')
íó´

However using 'unicode-escape' also works here as 'unicode-escape' assumes the bytes are encoded in 'latin1' and there are no unicode escapes in the OP's string:

>>> s1= "\xed\xf3\xb4\x90"
>>> print s1.decode('unicode-escape')  # same as: unicode(s1, 'unicode-escape')
íó´

在这种情况下,您可以使用latin1编解码器解码str

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM