简体   繁体   English

TypeError:initial_value必须是str还是none,而不是python 3中的字节?

[英]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.

相关问题 TypeError:initial_value必须为str或None,而不是字节(bigquery) - TypeError: initial_value must be str or None, not bytes (bigquery) 使用IMAP提取标头错误TypeError:initial_value必须为str或None,而不是字节 - Using IMAP to extract headers error TypeError: initial_value must be str or None, not bytes TypeError:initial_value必须是unicode或None,而不是str, - TypeError: initial_value must be unicode or None, not str, Python3 错误:initial_value 必须是 str 或 None,带有 StringIO - Python3 error: initial_value must be str or None, with StringIO StringIO initial_value必须为str,而不是Bytes - StringIO initial_value must be str, not Bytes 在Windows平台上,dateutil.parser.parse()给出错误“ initial_value必须为unicode或None,不是str” - dateutil.parser.parse() gives error “initial_value must be unicode or None, not str” on Windows platform Python3 类型错误:“值”必须是 str 或字节的实例,而不是元组 - Python3 TypeError: 'value' must be an instance of str or bytes, not a tuple Python - 类型错误:write() 参数必须是 str,而不是字节 - Python - TypeError: write() argument must be str, not bytes TypeError:rstrip arg必须为None或str:Python 3 - TypeError: rstrip arg must be None or str: Python 3 TypeError: 'value' 必须是 str 或 bytes 的实例,而不是浮点数 - TypeError: 'value' must be an instance of str or bytes, not a float
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM