python3 中 'utf-8' codec can't decode byte 0xc5的一个解决办法

在python3中,当我们对一个byte类型的数据流解码使之转换为string的时候,如果该byte不是’utf-8’编码的话就会出现类似的错误

解决方法是:

import chardet

data_str = data_byte.decode(chardet.detect(output)["encoding"])

其中chardet.detect(output)返回一个关于byte信息的字典,字典中关键字encoding对应的就是byte对应的编码方式

猜你喜欢

转载自blog.csdn.net/MSDN_tang/article/details/80384523