ascii codec cant decode byte 0xe9

后端 未结 4 496
说谎
说谎 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:39

    You are trying to encode bytestrings:

    >>> ''.encode('utf8')
    Traceback (most recent call last):
      File "", line 1, in 
    UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 20: ordinal not in range(128)
    

    Python is trying to be helpful, you can only encode a Unicode string to bytes, so to encode Python first implictly decodes, using the default encoding.

    The solution is to not encode data that is already encoded, or first decode using a suitable codec before trying to encode again, if the data was encoded to a different codec than what you needed.

    If you have a mix of unicode and bytestring values, decode just the bytestrings or encode just the unicode values; try to avoid mixing the types. The following decodes byte strings to unicode first:

    def ensure_unicode(v):
        if isinstance(v, str):
            v = v.decode('utf8')
        return unicode(v)  # convert anything not a string to unicode too
    
    output_string = u'\n'.join([ensure_unicode(line) for line in output_lines])
    

提交回复
热议问题