我制作了一个程序,该程序接收来自Gmail帐户的电子邮件,并保存其信息,特别是电子邮件的名称发件人,主题和正文作为纯文本。
我将此信息作为字符串获取,但问题是解码无法识别特殊字符(如重音符号)。我尝试用UTF-8,iso-8859-1和latin-1解码电子邮件的字节内容,但我无法正确解码特殊字符。
这是我的代码和一些打印示例。
result, email_data = mail.uid('fetch', latest_email_uid, '(RFC822)')
raw_email = email_data[0][1]
raw_email_string = raw_email.decode('iso-8859-1')
email_message = email.message_from_string(raw_email_string)
cuerpomensaje = email_message.get_payload()[0]
body = cuerpomensaje.get_payload()
textoplano = None
if type(body) == list:
textoplano = body[0].get_payload()
elif type(body) == str:
textoplano = body
emisor = email_message['From']
print(email.utils.parseaddr(emisor)[0])
print(email_message['Subject'])
print(textoplano)
打印时,我得到了这个:
=?UTF-8?Q?Andr=C3=A9s?=
=?UTF-8?Q?=C3=A1rbol_de_la_tierra?=
palabras con tilde como =C3=A1rbol
但我应该得到这个:
Andrés
árbol de la tierra
palabras con tilde como árbol
答案 0 :(得分:0)
let screensize = UIScreen.main.bounds.height
expenseLabel.frame.size.height = screensize*0.1
amountLabel.frame.size.height = screensize*0.1
dateLabel.frame.size.height = screensize*0.1
结果:
import re
import urllib.parse
def decode(text):
if text.startswith('=?UTF-8?Q?') and text.endswith('?='):
text = re.search('=\?UTF-8\?Q\?(.*)\?=', text).group(1)
return urllib.parse.unquote(text.replace('=', '%'))
您还可以使用包email.header
print(decode('=?UTF-8?Q?Andr=C3=A9s?='))
>>> Andrés
print(decode('=?UTF-8?Q?=C3=A1rbol_de_la_tierra?=').replace('_', ' '))
>>> árbol de la tierra
print(decode('palabras con tilde como =C3=A1rbol'))
>>> palabras con tilde como árbol