Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing Email BodyStructure in python
    primarykey
    data
    text
    <blockquote> <p><strong>Possible Duplicate:</strong><br> <a href="https://stackoverflow.com/questions/3973963/parsing-parenthesized-list-in-pythons-imaplib">parsing parenthesized list in python&#39;s imaplib</a> </p> </blockquote> <p>Python's email package lets you take the full text of an email message and parse it into parts and walk through the various parts. However, is there a library to parse the <code>BODYSTRUCTURE</code> response returned by the IMAP protocol into <code>email.message.Message</code> parts?</p> <p><strong>EDIT:</strong></p> <p>The equivalent method in PHP is <a href="http://www.php.net/manual/en/function.imap-fetchstructure.php" rel="nofollow noreferrer"><code>imap_fetchbody()</code></a>, which handles the parsing of structure automatically. </p> <p><strong>EDIT2</strong>:</p> <p>This question had been incorrectly closed as a duplicate. It is about parsing the <code>BODYSTRUCTURE</code> nested expression into a useful format (such as a dictionary) and not about parsing the raw string into python type. In any case, I ended up rolling my own solution and here's the code for anyone who comes across a similar problem in the future. It is intended to be used together with the <code>imapclient</code> library</p> <pre><code># ----- Parsing BODYSTRUCTURE into parts dictionaries ----- # def tuple2dict(pairs): """get dict from (key, value, key, value, ...) tuple""" if not pairs: return None return dict([(k, tuple2dict(v) if isinstance(v, tuple) else v) for k, v in zip(pairs[::2], pairs[1::2])]) def parse_singlepart(var, part_no): """convert non-multipart into dic""" # Basic fields for non-multipart (Required) part = dict(zip(['maintype', 'subtype', 'params', 'id', 'description', 'encoding', 'size'], var[:7]), part_no=part_no) part['params'] = tuple2dict(part['params']) # Type specific fields (Required for 'message' or 'text' type) index = 7 if part['maintype'].lower() == 'message' and part['subtype'].lower() == 'rfc822': part.update(zip(['envelope', 'bodystructure', 'lines'], var[7:10])) index = 10 elif part['maintype'].lower() == 'text': part['lines'] = var[7] index = 8 # Extension fields for non-multipart (Optional) part.update(zip(['md5', 'disposition', 'language', 'location'], var[index:])) part['disposition'] = tuple2dict(part['disposition']) return part def parse_multipart(var, part_no): """convert the multipart into dict""" part = { 'child_parts': [], 'part_no': part_no } # First parse the child parts index = 0 if isinstance(var[0], list): part['child_parts'] = [parse_part(v, ('%s.%d' % (part_no, i+1)).replace('TEXT.', '')) for i, v in enumerate(var[0])] index = 1 elif isinstance(var[0], tuple): while isinstance(var[index], tuple): part['child_parts'].append(parse_part(var[index], ('%s.%d' % (part_no, index+1)).replace('TEXT.', ''))) index += 1 # Then parse the required field subtype and optional extension fields part.update(zip(['subtype', 'params', 'disposition', 'language', 'location'], var[index:])) part['params'] = tuple2dict(part['params']) part['disposition'] = tuple2dict(part['disposition']) return part def parse_part(var, part_no=None): """Parse IMAP email BODYSTRUCTURE into nested dictionary See http://tools.ietf.org/html/rfc3501#section-6.4.5 for structure of email messages See http://tools.ietf.org/html/rfc3501#section-7.4.2 for specification of BODYSTRUCTURE """ if isinstance(var[0], (tuple, list)): return parse_multipart(var, part_no or 'TEXT') else: return parse_singlepart(var, part_no or '1') </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload