Я использую следующий код для экспорта всех писем в определенную папку Gmail.
Он работает хорошо, поскольку он вытягивает все электронные письма, которые я ожидаю, но он (или я), похоже, искажает кодировку для CR / newline.
Код:
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 () мог убрать детали. Я на самом деле не хочу флагов новой строки.
Я мог бы преобразовать всю строку в шестнадцатеричный формат и выполнить замену команды sorts / regex, но это похоже на убийство - когда проблема заключается в кодировании / чтении исходных данных
Что я вижу:
====
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.
====