【Python】成功解决 str object has no attribute decode

首先需要知道的是:解码的是字节流,需要声明字节流
在python3中,str已经不再使用decode()方法直接给str解码:str直接作为unicode,对bytes字符串需要声明

In [1]: '\xe4\xbd\xa0\xe5\xa5\xbd'.decode('utf8')

AttributeError: 'str' object has no attribute 'decode'

正确的写法:

In [2]: b'\xe4\xbd\xa0\xe5\xa5\xbd'.decode('utf8')

Out[2]: '你好'

你可能感兴趣的:(Python)