本文介绍了更改“引用可打印"编码为“utf-8"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 imaplib 阅读电子邮件.我收到了这个邮件正文:

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

那是 Quoted-printable 编码.
我需要从中获取 utf-8 .应该是Добрый день!

我在谷歌上搜索过,但 Python 的版本太混乱了.它在 Python 3 中已经是 unicode,我不能在这里使用 .encode('utf-8').

如何将其更改为 utf-8?

解决方案

quopri模块 可以将这些字节转换为未编码的字节流.然后您需要从它们所在的任何字符集中解码它们,然后编码回 utf-8.

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

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?

解决方案

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'))
Добрый день

这篇关于更改“引用可打印"编码为“utf-8"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 01:51