我正在尝试解析包含一些非ASCII标头的电子邮件,例如主题,来自等。
如果我尝试将电子邮件文件读取为二进制文件:
import email
mail = email.message_from_binary_file(open(file_path, "rb"))
我替换了所有字符(看起来像“ 电子邮件”包尝试将所有字符编码为ASCII):
>>> str(mail.get("Subject"))
'����������'
如果我尝试将消息读取为字符串:
mail = email.message_from_file(open(file_path))
我得到异常:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb1 in position 833: invalid start byte
使用Python 2,我能够通过将字符串作为字节获取,然后使用 chardet 定义编码来正确处理此类情况:
from chardet import detect as detect_encoding
encoding = detect_encoding(val)["encoding"]
decoded = val.decode(encoding)
有什么办法可以在Python 3中获得相同的行为?