Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
373 views
in Technique[技术] by (71.8m points)

python - Change "Quoted-printable" encoding to "utf-8"

I am trying to read email with imaplib. I get this mail body:

=C4=EE=E1=F0=FB=E9 =E4=E5=ED=FC!  

That is Quoted-printable encoding.
I need to get utf-8 from this. It should be Добрый день!

I googled it, but it is too messy with Python's versions. It is already unicode in Python 3, I cann't use .encode('utf-8') here.

How can I change this to utf-8?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The quopri module can convert those bytes to an unencoded byte stream. You need to then decode those from whatever character set they're in, then encode back to utf-8.

>>> b = quopri.decodestring('=C4=EE=E1=F0=FB=E9 =E4=E5=ED=FC')
>>> print(b.decode('windows-1251'))
Добрый день

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...