ascii codec cant decode byte 0xe9

后端 未结 4 495
说谎
说谎 2021-01-18 11:37

I have done some research and seen solutions but none have worked for me.

Python - 'ascii' codec can't decode byte

This didn\'t work for me. And

4条回答
  •  生来不讨喜
    2021-01-18 12:31

    A simple example of the problem is:

    >>> '\xe9'.encode('utf-8')
    Traceback (most recent call last):
      File "", line 1, in 
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xe9 in position 0: ordinal not in range(128)
    

    \xe9 isn't an ascii character which means that your string is already encoded. You need to decode it into python's unicode and then encode it again in the serialization format you want.

    Since I don't know where your string came from, I just peeked at the python codecs, picked something from Western Europe and gave it a go:

    >>> '\xe9'.decode('cp1252')
    u'\xe9'
    >>> u'\xe9'.encode('utf-8')
    '\xc3\xa9'
    >>> 
    

    You'll have the best luck if you know exactly which encoding the file came from.

提交回复
热议问题