'str'对象在Python3中没有属性'decode'

时间:2022-04-06 08:03:09

I've some problem with "decode" method in python 3.3.4. This is my code:

我在python 3.3.4中使用“decode”方法遇到了一些问题。这是我的代码:

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'

Do you have any ideas? Thanks

你有什么想法?谢谢

3 个解决方案

#1


14  

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')

Luckily open has an encoding argument which makes this easy:

幸运的是,open有一个编码参数,这使得这很简单:

for decodedLine in open('file', 'r', encoding='ISO-8859-1'):
    line = decodedLine.split('\t')

#2


1  

open already decodes to Unicode in Python 3 if you open in text mode. If you want to open it as bytes, so that you can then decode, you need to open with mode 'rb'.

如果在文本模式下打开,open已经在Python 3中解码为Unicode。如果要将其作为字节打开,以便可以解码,则需要使用模式'rb'打开。

#3


1  

This works for me smoothly to read Chinese text in Python 3.6. First, convert str to bytes, and then decode them.

这对我来说非常适合在Python 3.6中阅读中文文本。首先,将str转换为字节,然后解码它们。

for l in open('chinese2.txt','rb'):
    decodedLine = l.decode('gb2312')
    print(decodedLine)

#1


14  

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')

Luckily open has an encoding argument which makes this easy:

幸运的是,open有一个编码参数,这使得这很简单:

for decodedLine in open('file', 'r', encoding='ISO-8859-1'):
    line = decodedLine.split('\t')

#2


1  

open already decodes to Unicode in Python 3 if you open in text mode. If you want to open it as bytes, so that you can then decode, you need to open with mode 'rb'.

如果在文本模式下打开,open已经在Python 3中解码为Unicode。如果要将其作为字节打开,以便可以解码,则需要使用模式'rb'打开。

#3


1  

This works for me smoothly to read Chinese text in Python 3.6. First, convert str to bytes, and then decode them.

这对我来说非常适合在Python 3.6中阅读中文文本。首先,将str转换为字节,然后解码它们。

for l in open('chinese2.txt','rb'):
    decodedLine = l.decode('gb2312')
    print(decodedLine)