Note that there are some explanatory texts on larger screens.

plurals
  1. POPython - email header decoding UTF-8
    primarykey
    data
    text
    <p>is there any Python module which helps to decode the various forms of encoded mail headers, mainly Subject, to simple - say - UTF-8 strings?</p> <p>Here are example Subject headers from mail files that I have:</p> <pre><code>Subject: [ 201105311136 ]=?UTF-8?B?IMKnIDE2NSBBYnM=?=. 1 AO; Subject: [ 201105161048 ] GewSt:=?UTF-8?B?IFdlZ2ZhbGwgZGVyIFZvcmzDpHVmaWdrZWl0?= Subject: [ 201105191633 ] =?UTF-8?B?IERyZWltb25hdHNmcmlzdCBmw7xyIFZlcnBmbGVndW5nc21laHJhdWZ3ZW5kdW4=?= =?UTF-8?B?Z2VuIGVpbmVzIFNlZW1hbm5z?= </code></pre> <p>text - encoded sting - text</p> <p>text - encoded string</p> <p>text - encoded string - encoded string</p> <p>Encodig could also be something else like ISO 8859-15.</p> <p>Update 1: I forgot to mention, I tried email.header.decode_header</p> <pre><code> for item in message.items(): if item[0] == 'Subject': sub = email.header.decode_header(item[1]) logging.debug( 'Subject is %s' % sub ) </code></pre> <p>This outputs</p> <blockquote> <p>DEBUG:root:Subject is [('[ 201101251025 ] ELStAM;=?UTF-8?B?IFZlcmbDvGd1bmcgdm9tIDIx?=. Januar 2011', None)]</p> </blockquote> <p>which does not really help.</p> <p>Update 2: Thanks to Ingmar Hupp in the comments.</p> <p>the first example decodes to a list of two tupels: </p> <blockquote> <blockquote> <blockquote> <blockquote> <p>print decode_header("""[ 201105161048 ] GewSt:=?UTF-8?B?IFdlZ2ZhbGwgZGVyIFZvcmzDpHVmaWdrZWl0?=""")<br> [('[ 201105161048 ] GewSt:', None), (' Wegfall der Vorl\xc3\xa4ufigkeit', 'utf-8')]</p> </blockquote> </blockquote> </blockquote> </blockquote> <p>is this always [(string, encoding),(string, encoding), ...] so I need a loop to concat all the [0] items to one string or how to get it all in one string? </p> <blockquote> <p>Subject: [ 201101251025 ] ELStAM;=?UTF-8?B?IFZlcmbDvGd1bmcgdm9tIDIx?=. Januar 2011</p> </blockquote> <p>does not decode well:</p> <blockquote> <p>print decode_header("""[ 201101251025 ] ELStAM;=?UTF-8?B?IFZlcmbDvGd1bmcgdm9tIDIx?=. Januar 2011""")</p> <p>[('[ 201101251025 ] ELStAM;=?UTF-8?B?IFZlcmbDvGd1bmcgdm9tIDIx?=. Januar 2011', None)]</p> </blockquote>
    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.
 

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