By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,922 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,922 IT Pros & Developers. It's quick & easy.

Decode email subjects into unicode

P: n/a
Hi All,

'm in trouble with decoding email subjects. Here are some examples:
=?koi8-r?B?4tnT1NLP19nQz8zOyc3PIMkgzcHMz9rB1NLB1M7P?=
[Fwd: re:Flags Of The World, Us States, And Military]
=?ISO-8859-2?Q?=E9rdekes?=
=?UTF-8?B?aGliw6Fr?=

I know that "=?UTF-8?B" means UTF-8 + base64 encoding, but I wonder if
there is a standard method in the "email" package to decode these
subjects? I do not want to re-invent the weel.

Thanks,

Laszlo

Mar 18 '08 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Laszlo Nagy wrote:
I know that "=?UTF-8?B" means UTF-8 + base64 encoding, but I wonder if
there is a standard method in the "email" package to decode these
subjects?
The standard library function email.Header.decode_header will parse these
headers into an encoded bytestring paired with the appropriate encoding
specification, if any. For example:
>>raw_headers = [
.... '=?koi8-r?B?4tnT1NLP19nQz8zOyc3PIMkgzcHMz9rB1NLB1M7P?=',
.... '[Fwd: re:Flags Of The World, Us States, And Military]',
.... '=?ISO-8859-2?Q?=E9rdekes?=',
.... '=?UTF-8?B?aGliw6Fr?=',
.... ]
>>from email.Header import decode_header
for raw_header in raw_headers:
.... for header, encoding in decode_header(raw_header):
.... if encoding is None:
.... print header.decode()
.... else:
.... print header.decode(encoding)
....
Быстровыполнимо и малозатратно
[Fwd: re:Flags Of The World, Us States, And Military]
érdekes
hibák
Jeffrey
Mar 18 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.