<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"node595.html" />
<link rel=
"prev" href=
"module-email.Utils.html" />
<link rel=
"parent" href=
"module-email.html" />
<link rel=
"next" href=
"node595.html" />
<meta name='aesop' content='information'
/>
<title>12.2.10 Iterators
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"12.2.9 Miscellaneous utilities"
href=
"module-email.Utils.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"12.2 email "
href=
"module-email.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"12.2.11 Package History"
href=
"node595.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"module-email.Utils.html">12.2.9 Miscellaneous utilities
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-email.html">12.2 email
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node595.html">12.2.11 Package History
</A>
<!--End of Navigation Panel-->
<H2><A NAME=
"SECTION00142100000000000000000">
<A NAME=
"module-email.Iterators"></A>
Iterating over a message object tree is fairly easy with the
<tt class=
"method">Message.walk()
</tt> method. The
<tt class=
"module">email.Iterators
</tt> module
provides some useful higher level iterations over message object
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3957' xml:id='l2h-
3957'
class=
"function">body_line_iterator
</tt></b>(
</nobr></td>
<td><var>msg
</var><big>[
</big><var>, decode
</var><big>]
</big><var></var>)
</td></tr></table></dt>
This iterates over all the payloads in all the subparts of
<var>msg
</var>,
returning the string payloads line-by-line. It skips over all the
subpart headers, and it skips over any subpart with a payload that
isn't a Python string. This is somewhat equivalent to reading the
flat text representation of the message from a file using
<tt class=
"method">readline()
</tt>, skipping over all the intervening headers.
Optional
<var>decode
</var> is passed through to
<tt class=
"method">Message.get_payload()
</tt>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3958' xml:id='l2h-
3958'
class=
"function">typed_subpart_iterator
</tt></b>(
</nobr></td>
<td><var>msg
</var><big>[
</big><var>,
maintype
</var><big>[
</big><var>, subtype
</var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
This iterates over all the subparts of
<var>msg
</var>, returning only those
subparts that match the MIME type specified by
<var>maintype
</var> and
Note that
<var>subtype
</var> is optional; if omitted, then subpart MIME
type matching is done only with the main type.
<var>maintype
</var> is
optional too; it defaults to
<span class=
"mimetype">text
</span>.
Thus, by default
<tt class=
"function">typed_subpart_iterator()
</tt> returns each
subpart that has a MIME type of
<span class=
"mimetype">text/*
</span>.
The following function has been added as a useful debugging tool. It
should
<em>not
</em> be considered part of the supported public interface
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3959' xml:id='l2h-
3959'
class=
"function">_structure
</tt></b>(
</nobr></td>
<td><var>msg
</var><big>[
</big><var>, fp
</var><big>[
</big><var>, level
</var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
Prints an indented representation of the content types of the
message object structure. For example:
<div class=
"verbatim"><pre>
>>> msg = email.message_from_file(somefile)
>>> _structure(msg)
Optional
<var>fp
</var> is a file-like object to print the output to. It
must be suitable for Python's extended print statement.
<var>level
</var>
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"12.2.9 Miscellaneous utilities"
href=
"module-email.Utils.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"12.2 email "
href=
"module-email.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"12.2.11 Package History"
href=
"node595.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"module-email.Utils.html">12.2.9 Miscellaneous utilities
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-email.html">12.2 email
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"node595.html">12.2.11 Package History
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.