Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 íó´?

like image 519
rongdong.bai Avatar asked Mar 03 '26 10:03

rongdong.bai


2 Answers

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')
íó´
like image 92
Moinuddin Quadri Avatar answered Mar 04 '26 23:03

Moinuddin Quadri


In this case you can decode the str with the latin1 codec.

like image 20
John Machin Avatar answered Mar 04 '26 23:03

John Machin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!