unicode()参数2必须是string not Non

2024-04-20 12:28:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图解析包含html内容的电子邮件的内容。在

import imaplib
import email
....
    elif part.get_content_type() == "text/html":
        # if html is None:
        html = ""
        html += unicode(part.get_payload(decode=True),part.get_content_charset(),'replace').encode('utf8','replace')
        save_string = str("C:Dumpgmailemail2"+".eml")
        # location on disk
        myfile = open(save_string, 'a')
        myfile.write(str(html))
        #myfile.write(html.decode('utf-8'))
        myfile.close()

但这给了我一个错误:

^{pr2}$

Tags: import内容getstring电子邮件savehtmlcontent
1条回答
网友
1楼 · 发布于 2024-04-20 12:28:52

似乎part.get_content_charset()None,也许你可以为unicode()函数提供一些默认值-

html += unicode(part.get_payload(decode=True),part.get_content_charset() if part.get_content_charset() is not None else 'utf-8' ,'replace').encode('utf8','replace')

相关问题 更多 >