Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 296
Ilya Kogan Avatar asked Dec 12 '22 22:12

Ilya Kogan


1 Answers

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 סביח
like image 53
Martijn Pieters Avatar answered Jan 05 '23 16:01

Martijn Pieters