Skip to content Skip to sidebar Skip to footer

Python Imaplib: Display Non-ASCII Characters Correctly

I am using Python 3.5 and imaplib to fetch an e-mail from GMail and print its body. The body contains non-ASCII characters. These are 'encoded' in a strange way and I cannot find o

Solution 1:

The text is encoded with quoted-printable encoding, which is a way to encode non-ascii characters in ascii text. You can decode it using python's quopri module.

>>> import quopri
>>> bs = b'Gr=C3=BC=C3=9Fen'

>>> # Decode quoted-printable to raw bytes.
>>> utf8 = quopri.decodestring(bs)

>>> # Decode bytes to text.
>>> s = utf8.decode('utf-8')
>>> print(s)
Grüßen

You may find that quoted-printable is the value of the email's content-transfer-encoding header.


Post a Comment for "Python Imaplib: Display Non-ASCII Characters Correctly"