email.iterators: 迭代器?

源代碼: Lib/email/iterators.py


通過 Message.walk 方法來迭代消息對象樹是相當(dāng)容易的。 email.iterators 模塊提供了一些適用于消息對象樹的高層級迭代器。

email.iterators.body_line_iterator(msg, decode=False)?

此函數(shù)會迭代 msg 的所有子部分中的所有載荷,逐行返回字符串載荷。 它會跳過所有子部分的標(biāo)頭,并且它也會跳過任何包含不為 Python 字符串的載荷的子部分。 這基本上等價于使用 readline() 從一個文件讀取消息的純文本表示形式,并跳過所有中間的標(biāo)頭。

可選的 decode 會被傳遞給 Message.get_payload。

email.iterators.typed_subpart_iterator(msg, maintype='text', subtype=None)?

此函數(shù)會迭代 msg 的所有子部分,只返回其中與 maintypesubtype 所指定的 MIME 類型相匹配的子部分。

請注意 subtype 是可選項;如果省略,則僅使用主類型來進(jìn)行子部分 MIME 類型的匹配。 maintype 也是可選項;它的默認(rèn)值為 text

因此,在默認(rèn)情況下 typed_subpart_iterator() 會返回每一個 MIME 類型為 text/* 的子部分。

增加了以下函數(shù)作為有用的調(diào)試工具。 它 不應(yīng)當(dāng) 被視為該包所支持的公共接口的組成部分。

email.iterators._structure(msg, fp=None, level=0, include_default=False)?

打印消息對象結(jié)構(gòu)的內(nèi)容類型的縮進(jìn)表示形式。 例如:

>>>
>>> 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 是一個作為打印輸出目標(biāo)的文件類對象。 它必須適用于 Python 的 print() 函數(shù)。 level 是供內(nèi)部使用的。 include_default 如果為真值,則會同時打印默認(rèn)類型。