पर ध्यान दिए बिना इसे सादा पाठ में डीकोड करना मैं एक थंडरबर्ड एमबॉक्स फ़ाइल से ईमेल संदेशों के निकाय को निकालने के लिए पायथन 3 का उपयोग करने की कोशिश कर रहा हूं। यह एक आईएमएपी खाता है।एमबॉक्स फ़ाइल से ईमेल के निकाय को निकालने, चार्टसेट और कंटेंट ट्रांसफर एन्कोडिंग
मैं एक यूनिकोड स्ट्रिंग के रूप में प्रक्रिया के लिए उपलब्ध ईमेल के शरीर का टेक्स्ट हिस्सा रखना चाहता हूं। यह थंडरबर्ड में ईमेल जैसा दिखता है, और \ r \ n = 20 आदि जैसे बच निकले वर्णों को शामिल नहीं करना चाहिए
मुझे लगता है कि यह सामग्री स्थानांतरण एन्कोडिंग है जो मुझे नहीं पता कि कैसे डीकोड करना है या नहीं हटाना। मुझे विभिन्न सामग्री प्रकारों और विभिन्न सामग्री स्थानांतरण एन्कोडिंग के साथ ईमेल प्राप्त होते हैं। यह मेरे वर्तमान प्रयास है:
import mailbox
import quopri,base64
def myconvert(encoded,ContentTransferEncoding):
if ContentTransferEncoding == 'quoted-printable':
result = quopri.decodestring(encoded)
elif ContentTransferEncoding == 'base64':
result = base64.b64decode(encoded)
mboxfile = 'C:/Users/Username/Documents/Thunderbird/Data/profile/ImapMail/server.name/INBOX'
for msg in mailbox.mbox(mboxfile):
if msg.is_multipart(): #Walk through the parts of the email to find the text body.
for part in msg.walk():
if part.is_multipart(): # If part is multipart, walk through the subparts.
for subpart in part.walk():
if subpart.get_content_type() == 'text/plain':
body = subpart.get_payload() # Get the subpart payload (i.e the message body)
for k,v in subpart.items():
if k == 'Content-Transfer-Encoding':
cte = v # Keep the Content Transfer Encoding
elif subpart.get_content_type() == 'text/plain':
body = part.get_payload() # part isn't multipart Get the payload
for k,v in part.items():
if k == 'Content-Transfer-Encoding':
cte = v # Keep the Content Transfer Encoding
print(body)
print('Body is of type:',type(body))
body = myconvert(body,cte)
print(body)
लेकिन इस के साथ विफल:
Body is of type: <class 'str'>
Traceback (most recent call last):
File "C:/Users/David/Documents/Python/test2.py", line 31, in <module>
body = myconvert(body,cte)
File "C:/Users/David/Documents/Python/test2.py", line 6, in myconvert
result = quopri.decodestring(encoded)
File "C:\Python32\lib\quopri.py", line 164, in decodestring
return a2b_qp(s, header=header)
TypeError: 'str' does not support the buffer interface
कि अजीब बात है। get_payload() बाइट्स को वापस करना चाहिए, लेकिन पायथन 3 के नीचे str, जब तक कि आप 'decode = True' में पास न हों, जो आप नहीं करते हैं। –
मैंने इसे डीकोड = ट्रू के साथ अभी कोशिश की है, और यह बाइट्स लौटाता है, और इसलिए कोई त्रुटि नहीं है। ऐसा लगता है कि डिकोडिंग किया जाता है, और अब मुझे बस बाइट्स को स्ट्रिंग में कनवर्ट करना है। हालांकि मैंने अभी भी परीक्षण एन्कोडिंग की विस्तृत विविधता वाले ईमेल के साथ परीक्षण नहीं किया है। – dcb
हू, यह एक बग की तरह लगता है, यह चारों ओर एक और तरीका होना चाहिए, डीकोड = सही स्ट्र और डीकोड = झूठी बाइट्स लौटना चाहिए। :-) –