'str' 对象在 Python3 中没有属性 'decode' [英] 'str' object has no attribute 'decode' in Python3
本文介绍了'str' 对象在 Python3 中没有属性 'decode'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 python 3.3.4 中的解码"方法有一些问题.这是我的代码:
I've some problem with "decode" method in python 3.3.4. This is my code:
for lines in open('file','r'):
decodedLine = lines.decode('ISO-8859-1')
line = decodedLine.split('\t')
但我无法解码此问题的行:
But I can't decode the line for this problem:
AttributeError: 'str' object has no attribute 'decode'
你有什么想法吗?谢谢
推荐答案
一个编码字符串,一个解码字节.
One encodes strings, and one decodes bytes.
您应该从文件中读取字节并对其进行解码:
You should read bytes from the file and decode them:
for lines in open('file','rb'):
decodedLine = lines.decode('ISO-8859-1')
line = decodedLine.split('\t')
幸运的是 open
有一个编码参数,这使得这很容易:
Luckily open
has an encoding argument which makes this easy:
for decodedLine in open('file', 'r', encoding='ISO-8859-1'):
line = decodedLine.split('\t')
这篇关于'str' 对象在 Python3 中没有属性 'decode'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文