[英]TypeError: initial_value must be str or none, not bytes in python 3?
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]
parser = HeaderParser()
msg = parser.parsestr(header_data)
From this i get the error message: 从这里我得到错误信息:
TypeError: initial_value must be str or none, not bytes
Im using python 3 which apparently automatically decodes. 我使用python 3显然自动解码。 So why am i still getting this error message?
那么为什么我仍然收到此错误消息?
你可以试试:
header_data = data[1][0][1].decode('utf-8')
I would suggest do this,(Python 3) 我建议这样做,(Python 3)
typ, data = conn.fetch('1', '(RFC822)') # will read the first email
email_content = data[0][1]
msg = email.message_from_bytes(email_content) # this needs to be corrected in your case
emailDate = msg["Date"]
emaiSubject = msg["Subject"]
在这种情况下,您可能希望使用BytesHeaderParser 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.