2011-12-06 10 views
0

次のコードを使用して、特定のGmailフォルダ内のすべてのメールをエクスポートしています。Pythonを使用してGmailをエクスポートするimaplib - 改行の問題で文字列が変更される

私が期待しているすべての電子メールを引き出すという点ではうまくいっていますが、それはCR /改行のエンコーディングを混乱させるようです。

コード:

import imaplib 
import email 
import codecs 
mail = imaplib.IMAP4_SSL('imap.gmail.com') 
mail.login('[email protected]', '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」が散らばっオブジェクトを取得する時間が誤って改行やキャリッジリターンフラグを解釈していることです。

私は16進数[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. 
==== 

答えて

2

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を参照してください。

+0

おかげで、しかし... 'トレースバック(最新の呼び出しの最後): ファイル "gmail5.py"、17行、 email_message = email.message_from_string(raw_email).decode("引用さで-printable ") AttributeError:メッセージインスタンスに属性 'decode''がありません。@David K. Hess – Jay

+0

文字列を生成していると思いました。私はちょうど私の答えに追加されたstr()キャストを試してください。 –

+0

パーフェクト。ありがとうございました。夢のように働いた。 :) – Jay

0

これの1日の痛みを考えただけで2つの追加項目。 1ペイロードレベルでそれを行うと、メールからメールアドレスなどを取得するためにemail_messageを処理できます。

2文字セットをデコードする必要があります。ウェブページからHTMLをコピーして貼り付けている人、ワードドキュメントなどのコンテンツを処理しようとしていた電子メールに問題がありました。

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()) 

デイブ

関連する問題