UnicodeDecodeError on byte type

前端 未结 3 1078
醉酒成梦
醉酒成梦 2021-01-05 06:32

Using Python 3.4 I\'m getting the following error when trying to decode a byte type using utf-32

Traceback (most recent call last):
  File \"c:.\\SharqBot.py         


        
相关标签:
3条回答
  • 2021-01-05 07:00

    you can try with decode/encode('utf-16-le'). I tried it and it was OK to me. But I am not realy clear why. :P

    0 讨论(0)
  • 2021-01-05 07:02

    Every Unicode ordinal can be represented in UTF-8, if decodeing as UTF-8 isn't working, that's because the bytes being transmitted are in a different encoding, or the data is mixed text and binary data, and only some of it is UTF-8. Odds are the text is UTF-8 encoded (most network protocols are), so non-UTF-8 data would be framing data or the like, and would need to be parsed to extract the text data.

    Any attempt to mask such an error in the text/binary case would just be silencing problems, not fixing them. You need to know the encoding of the data (and the format, if it's not all text data with a single encoding), and use that. The data you receive doesn't magically become UTF-16 or UTF-32 because you want it to.

    0 讨论(0)
  • 2021-01-05 07:09

    try using encoding = 'ISO-8859-1'

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