Python Poplib 错误

标签 python email poplib

这是代码

import poplib
from email import parser

pop_conn = poplib.POP3_SSL('pop.gmail.com')
pop_conn.user('user@gmail.com')
pop_conn.pass_('password')
messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)]
messages = ["\n".join(mssg[1]) for mssg in messages]
messages = [parser.Parser().parsestr(mssg) for mssg in messages]
for message in messages:
    print(message)

当到达第 8 行时 (messages = ["\n".join(mssg[1]) for mssg in messages])

它是这样说的:

TypeError: sequence item 0: expected str instance, bytes found

有人知道我做错了什么吗?

最佳答案

使用 bytes.decode 将字节对象转换为字符串:

messages = ["\n".join(m.decode() for m in mssg[1]) for mssg in messages]

关于Python Poplib 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31724160/

相关文章:

python - 了解tornado协程装饰器+twitter认证

python - HttpResponseRedirect 反向不起作用 Django

Python:将 .txt 附加到电子邮件中

python - numpy.nditer 的正确用法?

python - PyTorch - 被覆盖的变量是否保留在图中?

java邮件smtp连接问题

email - Go:连接到 SMTP 服务器并在一个连接中发送多封电子邮件?

python - 在 Python 中使用 poplib 检查与收件箱不同的文件夹

python - 如果我使用 poplib 向 POP 服务器提供无效凭据,为什么我不能使用正确的凭据重试?