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

时间:2015-06-18 11:57:00

标签: python html email parsing unicode

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="myPackageName here" >

    <!-- Application permissions go here, mainly NFC -->
    <uses-permission android:name="android.permission.NFC" />
    <users-feature android:name="android.hardware.nfc" android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MyActivityNameHere"
            android:configChanges="orientation|keyboardHidden|screenSize"
            android:label="@string/app_name"
            android:theme="@style/FullscreenTheme" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <!-- NFC Intent Filters -->
            <intent-filter>
                <action android:name="android.nfc.action.TECH_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:mimeType="text/plain" />
            </intent-filter>

            <intent-filter>
                <action android:name="android.nfc.action.TAG_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT"/>
            </intent-filter>

            <meta-data android:name="android.nfc.action.TECH_DISCOVERED"
                android:resource="@xml/nfc_tech_filter" />
        </activity>
    </application>

</manifest>

但这给了我一个错误:

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()

1 个答案:

答案 0 :(得分:1)

似乎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')