Python ';str';对象没有属性';解码';在蟒蛇3中
我对Python3.3.4中的“decode”方法有一些问题。这是我的代码:Python ';str';对象没有属性';解码';在蟒蛇3中,python,python-3.x,python-3.3,Python,Python 3.x,Python 3.3,我对Python3.3.4中的“decode”方法有一些问题。这是我的代码: for lines in open('file','r'): decodedLine = lines.decode('ISO-8859-1') line = decodedLine.split('\t') 但我无法破解这个问题的线路: AttributeError: 'str' object has no attribute 'decode' 你有什么想法吗?谢谢一个人编码字符串,一个人解码字节 您
for lines in open('file','r'):
decodedLine = lines.decode('ISO-8859-1')
line = decodedLine.split('\t')
但我无法破解这个问题的线路:
AttributeError: 'str' object has no attribute 'decode'
你有什么想法吗?谢谢一个人编码字符串,一个人解码字节
您应该从文件中读取字节并对其进行解码:
for lines in open('file','rb'):
decodedLine = lines.decode('ISO-8859-1')
line = decodedLine.split('\t')
幸运的是,open
有一个编码参数,这使它变得简单:
for decodedLine in open('file', 'r', encoding='ISO-8859-1'):
line = decodedLine.split('\t')
open
如果以文本模式打开,则已在Python 3中解码为Unicode。如果你想以字节的形式打开它,这样你就可以解码了,你需要以“rb”模式打开它。这对我在Python3.6中阅读中文文本非常有效。首先,将str转换为字节,然后对其进行解码
for l in open('chinese2.txt','rb'):
decodedLine = l.decode('gb2312')
print(decodedLine)
在
pyjwt2.0.0
版本没有decode
方法之后,我们得到了这个错误。我们应该冻结以下版本以避免此问题
PyJWT==1.7.1
是的,Python3.x中的字符串不再具有
decode
方法-请看