Search code examples
pythonutf-8gmail-imap

How to decode this representation of a unicode string in Python?


I'm using imaplib to fetch subjects of email messages from Gmail, and some of the subjects look like this:

=?utf-8?Q?12_=D7=A1=D7=91=D7=99=D7=97?=

How can I decode this representation into a normal Unicode text?

Thanks in advance!


Solution

  • Your string is encoded using the Quoted-printable format for MIME headers. The email.header module handles this for you:

    >>> from email.header import decode_header
    >>> for part in decode_header('=?utf-8?Q?12_=D7=A1=D7=91=D7=99=D7=97?='):
    ...     print(str(*part))
    12 סביח