Python电子邮件包:如何可靠地将多部分邮件转换/解码为str

时间:2018-04-29 06:29:18

标签: python email character-encoding python-unicode

我试图用Python处理旧的,可能不合规的电子邮件。我可以毫无问题地阅读信息:

In [1]: m=email.message_from_binary_file(open('/path/to/problematic:2,S',mode='rb'))

但随后将其转换为字符串会产生UnicodeEncodeError:' gb2312'编解码器不能对字符' \ ufffd'进行编码。位置1238:非法多字节序列。这个有问题的消息的(多)部分有" Content-Type:text / plain;字符集=" GB2312"和" Content-Transfer-Encoding:8bit"。

In [2]: m.as_string()
---------------------------------------------------------------------------
UnicodeEncodeError                        Traceback (most recent call last)
<ipython-input-26-919a3a20e7d8> in <module>()
----> 1 m.as_string()

~/tools/conda/envs/conda3.6/lib/python3.6/email/message.py in as_string(self, unixfrom, maxheaderlen, policy)
    156                       maxheaderlen=maxheaderlen,
    157                       policy=policy)
--> 158         g.flatten(self, unixfrom=unixfrom)
    159         return fp.getvalue()
    160

~/tools/conda/envs/conda3.6/lib/python3.6/email/generator.py in flatten(self, msg, unixfrom, linesep)
    114                     ufrom = 'From nobody ' + time.ctime(time.time())
    115                 self.write(ufrom + self._NL)
--> 116             self._write(msg)
    117         finally:
    118             self.policy = old_gen_policy

~/tools/conda/envs/conda3.6/lib/python3.6/email/generator.py in _write(self, msg)
    179             self._munge_cte = None
    180             self._fp = sfp = self._new_buffer()
--> 181             self._dispatch(msg)
    182         finally:
    183             self._fp = oldfp

~/tools/conda/envs/conda3.6/lib/python3.6/email/generator.py in _dispatch(self, msg)
    212             if meth is None:
    213                 meth = self._writeBody
--> 214         meth(msg)
    215
    216     #

~/tools/conda/envs/conda3.6/lib/python3.6/email/generator.py in _handle_multipart(self, msg)
    270             s = self._new_buffer()
    271             g = self.clone(s)
--> 272             g.flatten(part, unixfrom=False, linesep=self._NL)
    273             msgtexts.append(s.getvalue())
    274         # BAW: What about boundaries that are wrapped in double-quotes?

~/tools/conda/envs/conda3.6/lib/python3.6/email/generator.py in flatten(self, msg, unixfrom, linesep)
    114                     ufrom = 'From nobody ' + time.ctime(time.time())
    115                 self.write(ufrom + self._NL)
--> 116             self._write(msg)
    117         finally:
    118             self.policy = old_gen_policy

~/tools/conda/envs/conda3.6/lib/python3.6/email/generator.py in _write(self, msg)
    179             self._munge_cte = None
    180             self._fp = sfp = self._new_buffer()
--> 181             self._dispatch(msg)
    182         finally:
    183             self._fp = oldfp

~/tools/conda/envs/conda3.6/lib/python3.6/email/generator.py in _dispatch(self, msg)
    212             if meth is None:
    213                 meth = self._writeBody
--> 214         meth(msg)
    215
    216     #

~/tools/conda/envs/conda3.6/lib/python3.6/email/generator.py in _handle_text(self, msg)
    241                 msg = deepcopy(msg)
    242                 del msg['content-transfer-encoding']
--> 243                 msg.set_payload(payload, charset)
    244                 payload = msg.get_payload()
    245                 self._munge_cte = (msg['content-transfer-encoding'],

~/tools/conda/envs/conda3.6/lib/python3.6/email/message.py in set_payload(self, payload, charset)
    313             if not isinstance(charset, Charset):
    314                 charset = Charset(charset)
--> 315             payload = payload.encode(charset.output_charset)
    316         if hasattr(payload, 'decode'):
    317             self._payload = payload.decode('ascii', 'surrogateescape')

UnicodeEncodeError: 'gb2312' codec can't encode character '\ufffd' in position 1238: illegal multibyte sequence

我对电子邮件内部的特性并不十分熟悉,在网上搜索这类错误时,主要是在抓取网页时出现问题,基本上建议有些明显:读入的原始字节包含Unicode字符无法使用目标编解码器进行编码。

我的问题是:可靠处理(可能是不合规的)电子邮件的正确方法是什么?

修改

有趣的是,m.get_payload(i=0).as_string()会触发相同的异常,但m.get_payload(i=0).get_payload(decode=False)会在我的终端上正确显示str,而m.get_payload(i=0).get_payload(decode=True)则会bytes } {(b'\xd7\xaa...')我无法解码。但是,错误发生在不同的字符

----> 1 m.get_payload(i=0).get_payload(decode=True).decode('gb2312')
UnicodeDecodeError: 'gb2312' codec can't decode byte 0xac in position 1995: illegal multibyte sequence

----> 1 m.get_payload(i=0).get_payload(decode=True).decode('gb18030')
UnicodeDecodeError: 'gb18030' codec can't decode byte 0xa3 in position 2033: illegal multibyte sequence

2 个答案:

答案 0 :(得分:1)

简短回答通常是bytes.decode来电error handlers。但细节取决于很多事情。

首先,您要对数据做什么?通常你需要一些绝对可逆的东西,所以你可以保证最坏的情况下你可以重新生成你所接受的东西,在这种情况下你可能需要surrogate-escape。在其他情况下,你想要生成一些人类可读的东西,并且最好跳过不可能的mojibake而不是尝试呈现它,所以ignore可能是正确的答案。等等。

其次,这是绝大多数消息是好的,但有些是错误的,或者许多消息大多数都很好但有一些错误?

最后,在某些情况下(对于遗留的中文编码尤其如此),实际问题只是某人指定了一个密切相关的字符集而不是他们实际使用的字符集。如果您正在查看的内容,您可能需要尝试编写显式回退代码:如果您遇到异常,请在常见错误的字典中查找编码并尝试其他编码。如果它们都不起作用,那么返回使用具有错误处理程序的特定编码。

答案 1 :(得分:0)

显然,如果Content-Transfer-Encoding8bitmessage.get_payload(decode=False)仍会尝试对其进行解码以恢复原始字节。另一方面,message.get_payload(decode=True)始终生成bytes,但实际解码仅在Content-Transfer-Encoding存在且quoted-printablebase64时才会发生。

我最终得到了以下代码。不确定这是否是处理电子邮件的正确方法。

body = []
if m.preamble is not None:
    body.extend(m.preamble.splitlines(keepends=True))

for part in m.walk():
    if part.is_multipart():
        continue

    ctype = part.get_content_type()
    cte = part.get_params(header='Content-Transfer-Encoding')
    if (ctype is not None and not ctype.startswith('text')) or \
       (cte is not None and cte[0][0].lower() == '8bit'):
        part_body = part.get_payload(decode=False)
    else:
        charset = part.get_content_charset()
        if charset is None or len(charset) == 0:
            charsets = ['ascii', 'utf-8']
        else:
            charsets = [charset]

        part_body = part.get_payload(decode=True)
        for enc in charsets:
            try:
                part_body = part_body.decode(enc)
                break
            except UnicodeDecodeError as ex:
                continue
            except LookupError as ex:
                continue
        else:
            part_body = part.get_payload(decode=False)

    body.extend(part_body.splitlines(keepends=True))

if m.epilogue is not None:
    body.extend(m.epilogue.splitlines(keepends=True))