Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

To decode the Subject =?iso-8859-2?Q?=... in email in python

606 views
Skip to first unread message

Dan Polansky

unread,
Apr 20, 2005, 3:30:35 AM4/20/05
to
When parsing messages using python's libraries email and mailbox, the
subject is often encoded using some kind of = notation. Apparently, the
encoding used in this notation is specified like =?iso-8859-2?Q?=... or
=?iso-8859-2?B?=. Is there a python library function to decode such a
subject, returning a unicode string? The use would be like

human_readable = cool_library.decode_equals(message['Subject'])

Thank you, Dan

Max M

unread,
Apr 20, 2005, 3:43:36 AM4/20/05
to


parts = email.Header.decode_header(header)
new_header = email.Header.make_header(parts)
human_readable = unicode(new_header)

--

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science

Roman Neuhauser

unread,
Apr 20, 2005, 3:54:11 AM4/20/05
to Dan Polansky, pytho...@python.org
# dan.po...@gmail.com / 2005-04-20 00:30:35 -0700:

> When parsing messages using python's libraries email and mailbox, the
> subject is often encoded using some kind of = notation. Apparently, the
> encoding used in this notation is specified like =?iso-8859-2?Q?=... or
> =?iso-8859-2?B?=.

That's RFC 2047 encoding, both examples introduce an ISO8859-2
string, the first variant says it's ascii-ized using
"Q"uoted-Printable, the other says the string is "B"ase64-encoded.

> Is there a python library function to decode such a
> subject, returning a unicode string? The use would be like
>
> human_readable = cool_library.decode_equals(message['Subject'])

quoting from http://docs.python.org/lib/module-email.Header.html

>>> from email.Header import decode_header
>>> decode_header('=?iso-8859-1?q?p=F6stal?=')
[('p\xf6stal', 'iso-8859-1')]

--
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man. You don't KNOW.
Cause you weren't THERE. http://bash.org/?255991

Neil Hodgson

unread,
Apr 20, 2005, 9:17:15 AM4/20/05
to
Dan Polansky:

Here is some code from a front end to Mailman moderation pages:

import email.Header
hdr = email.Header.make_header(email.Header.decode_header(sub))

Neil

Dan Polansky

unread,
Apr 22, 2005, 3:52:36 AM4/22/05
to
Max, thanks; that was helpful. Roman, your explanation was helpful as
well. Dan

0 new messages