Decode email subjects into unicode

L

Laszlo Nagy

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
 
J

Jeffrey Froman

Laszlo said:
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:
.... '=?koi8-r?B?4tnT1NLP19nQz8zOyc3PIMkgzcHMz9rB1NLB1M7P?=',
.... '[Fwd: re:Flags Of The World, Us States, And Military]',
.... '=?ISO-8859-2?Q?=E9rdekes?=',
.... '=?UTF-8?B?aGliw6Fr?=',
.... ].... 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top