email.iterators
:迭代器¶
使用 Message.walk
方法可以相当容易地遍历消息对象树。email.iterators
模块提供了一些在消息对象树上进行高级迭代的有用工具。
- email.iterators.body_line_iterator(msg, decode=False)¶
此函数会迭代 msg 的所有子部分中的所有有效负载,逐行返回字符串类型的有效负载。它会跳过所有子部分的标头,并跳过任何有效负载不是 Python 字符串的子部分。这有点类似于使用
readline()
从文件中读取消息的扁平化文本表示,并跳过所有中间的标头。可选的 decode 参数会传递给
Message.get_payload
。
- email.iterators.typed_subpart_iterator(msg, maintype='text', subtype=None)¶
此函数会迭代 msg 的所有子部分,仅返回 MIME 类型与 maintype 和 subtype 指定的类型相匹配的子部分。
请注意,subtype 是可选的;如果省略,则仅根据主类型匹配子部分的 MIME 类型。maintype 也是可选的,它默认为 text。
因此,默认情况下,
typed_subpart_iterator()
会返回每个 MIME 类型为 text/* 的子部分。
以下函数是作为一个有用的调试工具添加的。它不应被视为包支持的公共接口的一部分。
- email.iterators._structure(msg, fp=None, level=0, include_default=False)¶
打印消息对象结构的内容类型的缩进表示。例如
>>> msg = email.message_from_file(somefile) >>> _structure(msg) multipart/mixed text/plain text/plain multipart/digest message/rfc822 text/plain message/rfc822 text/plain message/rfc822 text/plain message/rfc822 text/plain message/rfc822 text/plain text/plain
可选的 fp 是一个用于打印输出的类文件对象。它必须适用于 Python 的
print()
函数。level 在内部使用。如果 include_default 为真,则也会打印默认类型。