2016-08-23 34 views
0

我是一個noob並使用Flanker解析郵件。 https://github.com/mailgun/flanker側面:MimePart不可迭代

我得到了一個N​​ot Iterable錯誤,我似乎無法弄清楚。我已經閱讀了大量關於列表的頁面,但我無法實現它的工作。我希望我能得到一些幫助。

要運行以下代碼,您需要安裝Flanker,並將該文件另存爲「email」。 http://pastebin.com/ZS4q2kYN

我正在嘗試閱讀'attachmenttype'並根據響應進行操作。不能讓它工作。下面是測試代碼:

#!/usr/bin/python 
#Open Email 
from flanker import mime 
with open ("email", mode="rb") as myfile: 
    message_string=myfile.read() 
myfile.close() 

#Read Email 
msg = mime.from_string(message_string) 

#read attachment type 
attachmenttype = msg.parts[1] 
print attachmenttype 

#This errors for me: TypeError: argument of type 'MimePart' is not iterable 
if attachmenttype: 
    if '(text/html)' in attachmenttype: 
     print "woohoo" 

這是我得到的迴應: myerror

在此先感謝。

回答

1

attachmenttype可能會打印一個字符串,但它不是一個字符串,更多的是包含一些屬性的結構。但是,由於您可以打印它,因此您已經完成了一半。只需使用str將其轉換爲字符串並比較即可。

修復你的代碼。我無法測試它,但我不明白它是如何工作的:

if attachmenttype: 
    if '(text/html)' in str(attachmenttype): 
     print("woohoo") 
+0

OMG THANK YOU!我昨天花了整整一天的時間,然後是星期五的大部分時間。哇...感謝您的快速回復 –

+0

你能解釋爲什麼這不是一個字符串? –

+0

這是一個有很多方法的對象。例如,text和html是這個類的獨立屬性,通過轉換爲字符串連接起來。 –