Trailing equal signs (=) in emails

痞子三分冷 提交于 2021-01-28 00:30:55

问题


I download messages from a Gmail account using POP3 and save them in a SQLite database for futher processing:

mailbox = poplib.POP3_SSL('pop.gmail.com', '995') 
mailbox.user(user) 
mailbox.pass_(password)

msgnum = mailbox.stat()[0]

for i in range(msgnum):
    msg = '\n'.join(mailbox.retr(i+1)[1])
    save_message(msg, dbmgr)

mailbox.quit()

However, looking in the database, all lines but the last one of the message body (payload) have trailing equal signs. Do you know why this happens?


回答1:


Frederic's link lead me to the answer. The encoding is called "quoted printable" (wiki) and it's possible to decode it using the quopri Python module (documentation):

msg.decode('quopri').decode('utf-8')



回答2:


Update for python 3.x

You now have to invoke the codecs module.

import codecs
bytes_msg = bytes(msg, 'utf-8')
decoded_msg = codecs.decode(bytes_msg, 'quopri').decode('utf-8')


来源:https://stackoverflow.com/questions/27421282/trailing-equal-signs-in-emails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!