Python-自动检测电子邮件内容编码 [英] Python - Auto Detect Email Content Encoding
本文介绍了Python-自动检测电子邮件内容编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写脚本来处理电子邮件,并且可以访问电子邮件的原始字符串内容.
I am writing a script to process emails, and I have access to the raw string content of the emails.
我目前正在寻找字符串"Content-Transfer-Encoding:",然后扫描紧随其后的字符,以确定编码.编码示例:base64或7bit或quoted-printable ..
I am currently looking for the string "Content-Transfer-Encoding:" and scanning the characters that follow immediately after, to determine the encoding. Example encodings: base64 or 7bit or quoted-printable ..
是否有更好的方法来自动确定电子邮件编码(至少是更Python化的方法)?
Is there a better way to automatically determine the email encoding(at least a more pythonic way)?
谢谢.
推荐答案
您可以使用以下标准Python包:电子邮件.
You may use this standard Python package: 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"]
请参见其他示例此处.
这篇关于Python-自动检测电子邮件内容编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文