Python - 自动检测电子邮件内容编码
Python - Auto Detect Email Content Encoding
我正在编写一个脚本来处理电子邮件,并且我可以访问电子邮件的原始字符串内容。
我目前正在寻找字符串 "Content-Transfer-Encoding:" 并扫描紧跟其后的字符,以确定编码。编码示例:base64 或 7bit 或 quoted-printable ..
是否有更好的方法来自动确定电子邮件编码(至少是更 pythonic 的方式)?
谢谢。
Python: Is there a way to determine the encoding of text file? 有一些很好的答案。基本上没有办法完全可靠地做到这一点,您使用的初始方法是最好的(应该首先检查),但如果不存在,那么有时有一些选项可以工作。
您可以使用此标准 Python 包:email。
例如:
import email
raw = """From: John Doe <example@example.com>
MIME-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable
Hi there!
"""
my_email = email.message_from_string(raw)
print my_email["Content-Transfer-Encoding"]
查看其他示例here。
我正在编写一个脚本来处理电子邮件,并且我可以访问电子邮件的原始字符串内容。
我目前正在寻找字符串 "Content-Transfer-Encoding:" 并扫描紧跟其后的字符,以确定编码。编码示例:base64 或 7bit 或 quoted-printable ..
是否有更好的方法来自动确定电子邮件编码(至少是更 pythonic 的方式)?
谢谢。
Python: Is there a way to determine the encoding of text file? 有一些很好的答案。基本上没有办法完全可靠地做到这一点,您使用的初始方法是最好的(应该首先检查),但如果不存在,那么有时有一些选项可以工作。
您可以使用此标准 Python 包:email。
例如:
import email
raw = """From: John Doe <example@example.com>
MIME-Version: 1.0
Content-Type: text/plain
Content-Transfer-Encoding: quoted-printable
Hi there!
"""
my_email = email.message_from_string(raw)
print my_email["Content-Transfer-Encoding"]
查看其他示例here。