1

这是代码

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

有谁知道我做错了什么?

4

1 回答 1

1

使用以下命令将字节对象转换为字符串bytes.decode

messages = ["\n".join(m.decode() for m in mssg[1]) for mssg in messages]
于 2015-07-30T12:49:21.933 回答