“str”对象没有“decode”属性。Python 3错误呢?

时间:2021-12-15 07:48:41

Here is my code:

这是我的代码:

import imaplib
from email.parser import HeaderParser

conn = imaplib.IMAP4_SSL('imap.gmail.com')
conn.login('example@gmail.com', 'password')
conn.select()
conn.search(None, 'ALL')
data = conn.fetch('1', '(BODY[HEADER])')
header_data = data[1][0][1].decode('utf-8')

at this point I get the error message

此时,我得到了错误消息。

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

Python 3 doesn't have decode anymore, am I right? how can I fix this?

Python 3不再有解码了,对吗?我该怎么解决这个问题呢?

Also, in:

另外,在:

data = conn.fetch('1', '(BODY[HEADER])')

I am selecting only the 1st email. How do I select all?

我只选择了第一封邮件。我如何选择所有?

4 个解决方案

#1


72  

You are trying to decode an object that is already decoded. You have a str, there is no need to decode from UTF-8 anymore.

您正在尝试解码一个已经被解码的对象。你有一个str,不再需要解码UTF-8。

Simply drop the .decode('utf-8') part:

只需删除.decode('utf-8')部分:

header_data = data[1][0][1]

As for your fetch() call, you are explicitly asking for just the first message. Use a range if you want to retrieve more messages. See the documentation:

至于fetch()调用,您只是明确地请求第一个消息。如果希望检索更多消息,请使用范围。看到文档:

The message_set options to commands below is a string specifying one or more messages to be acted upon. It may be a simple message number ('1'), a range of message numbers ('2:4'), or a group of non-contiguous ranges separated by commas ('1:3,6:9'). A range can contain an asterisk to indicate an infinite upper bound ('3:*').

下面命令的message_set选项是一个字符串,指定要操作的一个或多个消息。它可以是一个简单的消息数('1')、一个消息数范围('2:4'),或者一组由逗号分隔的非连续范围('1:3,6:9')。范围可以包含星号来表示无限的上限('3:*')。

#2


13  

Begin with Python 3, all string is unicode object.

从Python 3开始,所有字符串都是unicode对象。

  a = 'Happy New Year' # Python 3
  b = unicode('Happy New Year') # Python 2

the code before are same. So I think you should remove the .decode('utf-8'). Because you have already get the unicode object.

之前的代码是一样的。所以我认为你应该去掉。decode(utf-8)。因为您已经获得了unicode对象。

#3


7  

Use it by this Method:

使用方法如下:

str.encode().decode()

#4


2  

I'm not familiar with the library, but if your problem is that you don't want a byte array, one easy way is to specify an encoding type straight in a cast:

我不熟悉这个库,但是如果您的问题是您不想要一个字节数组,一个简单的方法是直接在cast中指定一个编码类型:

>>> my_byte_str
b'Hello World'

>>> str(my_byte_str, 'utf-8')
'Hello World'

#1


72  

You are trying to decode an object that is already decoded. You have a str, there is no need to decode from UTF-8 anymore.

您正在尝试解码一个已经被解码的对象。你有一个str,不再需要解码UTF-8。

Simply drop the .decode('utf-8') part:

只需删除.decode('utf-8')部分:

header_data = data[1][0][1]

As for your fetch() call, you are explicitly asking for just the first message. Use a range if you want to retrieve more messages. See the documentation:

至于fetch()调用,您只是明确地请求第一个消息。如果希望检索更多消息,请使用范围。看到文档:

The message_set options to commands below is a string specifying one or more messages to be acted upon. It may be a simple message number ('1'), a range of message numbers ('2:4'), or a group of non-contiguous ranges separated by commas ('1:3,6:9'). A range can contain an asterisk to indicate an infinite upper bound ('3:*').

下面命令的message_set选项是一个字符串,指定要操作的一个或多个消息。它可以是一个简单的消息数('1')、一个消息数范围('2:4'),或者一组由逗号分隔的非连续范围('1:3,6:9')。范围可以包含星号来表示无限的上限('3:*')。

#2


13  

Begin with Python 3, all string is unicode object.

从Python 3开始,所有字符串都是unicode对象。

  a = 'Happy New Year' # Python 3
  b = unicode('Happy New Year') # Python 2

the code before are same. So I think you should remove the .decode('utf-8'). Because you have already get the unicode object.

之前的代码是一样的。所以我认为你应该去掉。decode(utf-8)。因为您已经获得了unicode对象。

#3


7  

Use it by this Method:

使用方法如下:

str.encode().decode()

#4


2  

I'm not familiar with the library, but if your problem is that you don't want a byte array, one easy way is to specify an encoding type straight in a cast:

我不熟悉这个库,但是如果您的问题是您不想要一个字节数组,一个简单的方法是直接在cast中指定一个编码类型:

>>> my_byte_str
b'Hello World'

>>> str(my_byte_str, 'utf-8')
'Hello World'