How to change a string to Unicode in Python 2?

前端 未结 2 1311
难免孤独
难免孤独 2020-12-12 04:30

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

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

How co

相关标签:
2条回答
  • 2020-12-12 05:10

    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')
    íó´
    
    0 讨论(0)
  • 2020-12-12 05:22

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

    0 讨论(0)
提交回复
热议问题