How do I decode unicode characters in the subject using email.message_from_string()?

John Machin sjmachin at lexicon.net
Tue Feb 24 19:52:15 EST 2009


On Feb 25, 11:07 am, "Roy H. Han" <starsareblueandfara... at gmail.com>
wrote:
> Dear python-list,
>
> I'm having some trouble decoding an email header using the standard
> imaplib.IMAP4 class and email.message_from_string method.
>
> In particular, email.message_from_string() does not seem to properly
> decode unicode characters in the subject.
>
> How do I decode unicode characters in the subject?

You don't. You can't. You decode str objects into unicode objects. You
encode unicode objects into str objects. If your input is not a str
object, you have a problem.

I'm no expert on the email package, but experts don't have crystal
balls, so let's gather some data for them while we're waiting for
their timezones to align:

Presumably your code is doing something like:
   msg = email.message_from_string(a_string)

Please report the results of
   print repr(a_string)
and
   print type(msg)
   print msg.items()
and tell us what you expected.

Cheers,
John



More information about the Python-list mailing list