python - unicode() 参数 2 必须是字符串而不是 None

标签 python html email parsing unicode

我正在尝试解析包含 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()

但这给了我一个错误:

Traceback (most recent call last):
  File "extract.py", line 22, in <module>
    html += unicode(part.get_payload(decode=True),part.get_content_charset(),"replace").encode('utf8','replace')
TypeError: unicode() argument 2 must be string, not None

最佳答案

似乎 part.get_content_charset()None ,如果它的 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')

关于python - unicode() 参数 2 必须是字符串而不是 None,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30914725/

相关文章:

php - php联系表中的回复地址

java - 解析电子邮件

python - 使用 Python 从数据框的列中删除停用词

javascript - 是否可以在 javascript 中创建一个在刷新页面时不会改变的变量?

javascript - 强制浏览器以纵向加载页面

html - 如何在 HTML 中表示负数

Azure B2C 使用策略配置 OTP 参数

python - 根据其他列值/Pandas -Python 在数据框中创建 ID 列

python - "train_test_split"的模型评估不是静态的?

python - 这可以变得更 pythonic 吗?