email.iterators:疊代器

原始碼:Lib/email/iterators.py


Iterating over a message object tree is fairly easy with theMessage.walk method. Theemail.iterators module provides some useful higher level iterations overmessage object trees.

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

This iterates over all the payloads in all the subparts ofmsg, returning thestring payloads line-by-line. It skips over all the subpart headers, and itskips over any subpart with a payload that isn't a Python string. This issomewhat equivalent to reading the flat text representation of the message froma file usingreadline(), skipping over all theintervening headers.

Optionaldecode is passed through toMessage.get_payload.

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

This iterates over all the subparts ofmsg, returning only those subparts thatmatch the MIME type specified bymaintype andsubtype.

Note thatsubtype is optional; if omitted, then subpart MIME type matching isdone only with the main type.maintype is optional too; it defaults totext.

Thus, by defaulttyped_subpart_iterator() returns each subpart that has aMIME type oftext/*.

The following function has been added as a useful debugging tool. It shouldnot be considered part of the supported public interface for the package.

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

Prints an indented representation of the content types of the message objectstructure. For example:

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

Optionalfp is a file-like object to print the output to. It must besuitable for Python'sprint() function.level is used internally.include_default, if true, prints the default type as well.