【问题标题】:Python - Auto Detect Email Content EncodingPython - 自动检测电子邮件内容编码
【发布时间】:2017-01-07 04:58:51
【问题描述】:

我正在编写一个脚本来处理电子邮件,并且我可以访问电子邮件的原始字符串内容。

我目前正在寻找字符串“Content-Transfer-Encoding:”并扫描紧随其后的字符以确定编码。示例编码:base64 或 7bit 或quoted-printable ..

有没有更好的方法来自动确定电子邮件编码(至少是一种更 Python 的方式)?

谢谢。

【问题讨论】:

    标签: python email encoding html-email


    【解决方案1】:

    Python: Is there a way to determine the encoding of text file? 有一些很好的答案。基本上没有办法完全可靠地做到这一点,并且您使用的初始方法是最好的(应该先检查),但如果它不存在,那么有时会有一些选项可以工作。

    【讨论】:

      【解决方案2】:

      您可以使用这个标准的 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

      【讨论】:

        猜你喜欢
        • 2017-11-12
        • 1970-01-01
        • 2016-01-15
        • 1970-01-01
        • 2012-03-04
        • 2015-12-06
        • 2020-08-06
        • 1970-01-01
        • 2010-11-04
        相关资源
        最近更新 更多