我正在編寫腳本來處理電子郵件,並且我可以訪問電子郵件的原始字符串內容。Python - 自動檢測電子郵件內容編碼
我目前正在尋找字符串「Content-Transfer-Encoding:」並掃描緊隨其後的字符,以確定編碼。示例編碼:base64或7bit或quoted-printable ..
有沒有更好的方法來自動確定電子郵件編碼(至少更pythonic的方式)?
謝謝。
我正在編寫腳本來處理電子郵件,並且我可以訪問電子郵件的原始字符串內容。Python - 自動檢測電子郵件內容編碼
我目前正在尋找字符串「Content-Transfer-Encoding:」並掃描緊隨其後的字符,以確定編碼。示例編碼:base64或7bit或quoted-printable ..
有沒有更好的方法來自動確定電子郵件編碼(至少更pythonic的方式)?
謝謝。
你可以使用這個標準的Python包:email。
例如:
import email
raw = """From: John Doe <[email protected]>
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。
Python: Is there a way to determine the encoding of text file?有一些很好的答案。基本上沒有辦法可靠地完成它,而且你使用的最初方法是最好的(應該先檢查),但如果它不存在,那麼有時候有幾個選項可以工作。