【问题标题】:Export Gmail using Python imaplib - text mangled with newline issues使用 Python imaplib 导出 Gmail - 文本出现换行问题
【发布时间】:2011-12-06 01:24:12
【问题描述】:

我正在使用以下代码导出特定 gmail 文件夹中的所有电子邮件。

它运行良好,因为它会提取我期望的所有电子邮件,但它(或我)似乎破坏了 CR / 换行符的编码。

代码:

import imaplib
import email
import codecs
mail = imaplib.IMAP4_SSL('imap.gmail.com')
mail.login('myUser@gmail.com', 'myPassword')  #user / password
mail.list()
mail.select("myFolder") # connect to folder with matching label

result, data = mail.uid('search', None, "ALL") # search and return uids instead
i = len(data[0].split())

for x in range(i):
    latest_email_uid = data[0].split()[x]
    result, email_data = mail.uid('fetch', latest_email_uid, '(RFC822)')
    raw_email = email_data[0][1]
    email_message = email.message_from_string(raw_email)
    save_string = str("C:\\\googlemail\\boxdump\\email_" + str(x) + ".eml") #set to   save location
    myfile = open(save_string, 'a')
    myfile.write(email_message)
    myfile.close()

我的问题是,当我到达对象时,它到处都是“= 0A”,我假设它被错误地解释为换行符或回车标志。

我可以在十六进制中找到它,[d3 03 03 0a] 但因为这不是“字符”,所以我找不到 str.replace() 取出零件的方法。我实际上并不想要换行标志。

我可以将整个字符串转换为十六进制,并做一个替换排序/正则表达式的事情,但这似乎太过分了 - 当问题在于源数据的编码/读取时

我看到了什么:

====
CAUTION:  This email message and any attachments con= tain information that may be confidential and may be LEGALLY PRIVILEGED. If yo= u are not the intended recipient, any use, disclosure or copying of this messag= e or attachments is strictly prohibited. If you have received this email messa= ge in error please notify us immediately and erase all copies of the message an= d attachments. Thank you.
==== 

我想要什么:

====
CAUTION:  This email message and any attachments contain information that may be confidential and may be LEGALLY PRIVILEGED. If you are not the intended recipient, any use, disclosure or copying of this message or attachments is strictly prohibited. If you have received this email message in error please notify us immediately and erase all copies of the message and attachments. Thank you.
====  

【问题讨论】:

    标签: python gmail newline imaplib


    【解决方案1】:

    你看到的是Quoted Printable编码。

    尝试改变:

    email_message = email.message_from_string(raw_email)
    

    到:

    email_message = str(email.message_from_string(raw_email)).decode("quoted-printable")
    

    有关详细信息,请参阅 Python 编解码器模块中的 Standard Encodings

    【讨论】:

    • 谢谢,但是...Traceback (most recent call last): File "gmail5.py", line 17, in <module> email_message = email.message_from_string(raw_email).decode("quoted-printable") AttributeError: Message instance has no attribute 'decode'@David K. Hess
    • 糟糕,我以为这是在产生一个字符串。试试我刚刚添加到答案中的 str() 演员。
    • 完美。谢谢你。像梦一样工作。 :)
    【解决方案2】:

    只有 2 个额外的项目已经考虑了这一天的痛苦。 1 在有效负载级别执行此操作,以便您可以处理 email_message 以从您的邮件中获取电子邮件地址等。

    2 您还需要对字符集进行解码,当人们将网页中的 html 和 word 文档中的内容等复制并粘贴到我正在尝试处理的电子邮件中时,我遇到了麻烦。

    if maintype == 'multipart':
                        for part in email_message.get_payload():
                                if part.get_content_type() == 'text/plain':
                                    text += part.get_payload().decode("quoted-printable").decode(part.get_content_charset())
    

    希望这对某人有所帮助!

    戴夫

    【讨论】:

      猜你喜欢
      • 2017-07-09
      • 2011-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-06
      • 2010-12-12
      • 1970-01-01
      相关资源
      最近更新 更多