'str' 对象没有属性 'decode'。Python 3 错误?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28583565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:28:59  来源:igfitidea点击:

'str' object has no attribute 'decode'. Python 3 error?

pythonpython-3.ximaplib

提问by

Here is my code:

这是我的代码:

import imaplib
from email.parser import HeaderParser

conn = imaplib.IMAP4_SSL('imap.gmail.com')
conn.login('[email protected]', '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?

我只选择第一封电子邮件。怎么全选?

采纳答案by Martijn Pieters

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_setoptions 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:*')。

回答by Neo Ko

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对象。

回答by Alireza

Use it by this Method:

通过这种方法使用它:

str.encode().decode()

回答by Broper

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:

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

>>> my_byte_str
b'Hello World'

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

回答by Aditya

It s already decoded in Python3, Try directly it should work.

它已经在Python3中解码了,直接尝试它应该可以工作。

回答by krishna chandak

For Python3

对于 Python3

html = """\u003Cdiv id=\u0022contenedor\u0022\u003E \u003Ch2 class=\u0022text-left m-b-2\u0022\u003EInformaci\u00f3n del veh\u00edculo de patente AA345AA\u003C\/h2\u003E\n\n\n\n \u003Cdiv class=\u0022panel panel-default panel-disabled m-b-2\u0022\u003E\n \u003Cdiv class=\u0022panel-body\u0022\u003E\n \u003Ch2 class=\u0022table_title m-b-2\u0022\u003EInformaci\u00f3n del Registro Automotor\u003C\/h2\u003E\n \u003Cdiv class=\u0022col-md-6\u0022\u003E\n \u003Clabel class=\u0022control-label\u0022\u003ERegistro Seccional\u003C\/label\u003E\n \u003Cp\u003ESAN MIGUEL N\u00b0 1\u003C\/p\u003E\n \u003Clabel class=\u0022control-label\u0022\u003EDirecci\u00f3n\u003C\/label\u003E\n \u003Cp\u003EMAESTRO ANGEL D\u0027ELIA 766\u003C\/p\u003E\n \u003Clabel class=\u0022control-label\u0022\u003EPiso\u003C\/label\u003E\n \u003Cp\u003EPB\u003C\/p\u003E\n \u003Clabel class=\u0022control-label\u0022\u003EDepartamento\u003C\/label\u003E\n \u003Cp\u003E-\u003C\/p\u003E\n \u003Clabel class=\u0022control-label\u0022\u003EC\u00f3digo postal\u003C\/label\u003E\n \u003Cp\u003E1663\u003C\/p\u003E\n \u003C\/div\u003E\n \u003Cdiv class=\u0022col-md-6\u0022\u003E\n \u003Clabel class=\u0022control-label\u0022\u003ELocalidad\u003C\/label\u003E\n \u003Cp\u003ESAN MIGUEL\u003C\/p\u003E\n \u003Clabel class=\u0022control-label\u0022\u003EProvincia\u003C\/label\u003E\n \u003Cp\u003EBUENOS AIRES\u003C\/p\u003E\n \u003Clabel class=\u0022control-label\u0022\u003ETel\u00e9fono\u003C\/label\u003E\n \u003Cp\u003E(11)46646647\u003C\/p\u003E\n \u003Clabel class=\u0022control-label\u0022\u003EHorario\u003C\/label\u003E\n \u003Cp\u003E08:30 a 12:30\u003C\/p\u003E\n \u003C\/div\u003E\n \u003C\/div\u003E\n\u003C\/div\u003E \n\n\u003Cp class=\u0022text-center m-t-3 m-b-1 hidden-print\u0022\u003E\n \u003Ca href=\u0022javascript:window.print();\u0022 class=\u0022btn btn-default\u0022\u003EImprim\u00ed la consulta\u003C\/a\u003E \u0026nbsp; \u0026nbsp;\n \u003Ca href=\u0022\u0022 class=\u0022btn use-ajax btn-primary\u0022\u003EHacer otra consulta\u003C\/a\u003E\n\u003C\/p\u003E\n\u003C\/div\u003E"""
print(html.replace("\/", "/").encode().decode('unicode_escape'))

回答by demongolem

Other answers sort of hint at it, but the problem may arise from expecting a bytes object. In Python 3, decode is valid when you have an object of class bytes. Running encode before decode may "fix" the problem, but it is a useless pair of operations that suggest the problem us upstream.

其他答案有点暗示它,但问题可能是由于期望字节对象而引起的。在 Python 3 中,当您拥有字节类对象时,解码是有效的。在解码之前运行 encode 可能会“解决”问题,但这是一对无用的操作,向我们上游提出问题。