本文介绍了Python电子邮件有效解码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我知道这个问题已经被问到了一千次,但是我很紧张,所以我不禁要求帮助。



我有一个电子邮件与法国口音caractères。句子是:



céline:Berlin Annette:0633'。



python的电子邮件包更改



':'on'= 3A'



éon= E9

如何回到口音??和=标志?



我尝试了几件事, net:



获取有效载荷:

 >> ; z = msg.get_payload()
>>> z
'C = E9line = 3A柏林Annette = 3A 0633'
>>>信息(z)
(< type'str'>''C = E9line = 3A Berlin Annette = 3A 0633')

通过字符集解码:

 >>> z = msg.get_payload()。解码(msg.get_content_charset())
>>> z
u'C = E9line = 3A柏林Annette = 3A 0633'
>>>信息(z)
(< type'unicode'> u''C = E9line = 3A Berlin Annette = 3A 0633')

或遭遇解码后在utf_8中修改:

 >>> z = msg.get_payload()。decode(msg.get_content_charset())。encode('utf-8')
>>> z
'C = E9line = 3A柏林Annette = 3A 0633'
>>>信息(z)
(< type'str'>'C = E9line = 3A Berlin Annette = 3A 0633')

我也尝试过urllib:

  urllib.unquote(z)
' C = E9line = 3A 00493039746784 Berlin Annette = 3A 0633'

似乎没有任何工作:(

解决方案

您可以使用,它会做以上对于你:

  msg.get_payload(decode = True)


I know this question has been asked thousand of time, but I am near to a nervous break so I can't help but to ask for help.

I have an email with french accents caractères. The sentence is :

"céline : Berlin Annette : 0633'.

The email package of python changes

':' on '=3A'

"é" on "=E9".

How to get back to the accent ?? and to the "=" sign ?

I tried several things looking through the net :

getting the payload :

>>> z = msg.get_payload()
>>> z
'C=E9line =3A Berlin Annette =3A 0633'
>>> infos(z)
(<type 'str'>, '  'C=E9line =3A Berlin Annette =3A 0633')

decoding it by its charset:

>>> z = msg.get_payload().decode(msg.get_content_charset())
>>> z
u'  C=E9line =3A Berlin Annette =3A 0633'
>>> infos(z)
(<type 'unicode'>, u'  'C=E9line =3A Berlin Annette =3A 0633')

or encoding it in utf_8 after decoding:

>>> z = msg.get_payload().decode(msg.get_content_charset()).encode('utf-8')
>>> z
  'C=E9line =3A Berlin Annette =3A 0633'
>>> infos(z)
(<type 'str'>,   'C=E9line =3A Berlin Annette =3A 0633')

I also tried urllib:

urllib.unquote(z)
'C=E9line =3A 00493039746784 Berlin Annette =3A 0633'

nothing seems to work :(

解决方案

You can use quopri.decodestring to decode the string.

>>> quopri.decodestring('C=E9line =3A 00493039746784 Berlin Annette =3A 0633')
'C\xe9line : 00493039746784 Berlin Annette : 0633'

If you pass decode=True to Message.get_payload, it will do above for you:

msg.get_payload(decode=True)

这篇关于Python电子邮件有效解码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 16:45