Python:遇到等号时,邮件get_payload解码失败? [英] Python: email get_payload decode fails when hitting equal sign?

查看:2843
本文介绍了Python:遇到等号时,邮件get_payload解码失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用get_payload进入陌生人:当它在解码消息中看到等号时,似乎会消失。以下是显示错误的代码:

  import email 

data = file('testmessage.txt' ).read()
msg = email.message_from_string(data)
payload = msg.get_payload(decode = True)
打印有效负载
pre>

这里有一个示例消息:测试消息



消息仅在第一个=之前打印。其余省略。任何人都知道发生了什么?



与decode = False相同的脚本返回完整的消息,所以看起来解码不符合等号。 >

这是Python 2.5。

解决方案

你有一个行结束问题。测试消息的正文使用裸机回车(\r),不带换行符(\\\
)。如果您在解析电子邮件之前修复了行尾,那么这一切都可以正常工作:

 导入电子邮件,re 
data = file('testmessage.txt')。read()
data = re.sub(r'\r(?!\\\
)','\r\\\
',data)#Bare \\ \\ r变成\r\\\

msg = email.message_from_string(data)
payload = msg.get_payload(decode = True)
打印有效负载


Running into strangeness with get_payload: it seems to crap out when it sees an equal sign in the message it's decoding. Here's code that displays the error:

import email

data = file('testmessage.txt').read()
msg  = email.message_from_string( data )
payload = msg.get_payload(decode=True)
print payload

And here's a sample message: test message.

The message is printed only until the first "=" . The rest is omitted. Anybody know what's going on?

The same script with "decode=False" returns the full message, so it appears the decode is unhappy with the equal sign.

This is under Python 2.5 .

解决方案

You have a line endings problem. The body of your test message uses bare carriage returns (\r) without newlines (\n). If you fix up the line endings before parsing the email, it all works:

import email, re
data = file('testmessage.txt').read()
data = re.sub(r'\r(?!\n)', '\r\n', data)  # Bare \r becomes \r\n
msg  = email.message_from_string( data )
payload = msg.get_payload(decode=True)
print payload

这篇关于Python:遇到等号时,邮件get_payload解码失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆