Extract just email headers in python(在 python 中仅提取电子邮件标头)
问题描述
我在尝试在 python 中提取所有电子邮件标头时遇到了一些问题.我知道如何获取我正在寻找的那些,但我想保存所有标题,但我不知道该怎么做.
I'm having some issues trying to extract all the email headers in python. I know how to get the ones I'm looking for but I want to save all the headers and I'm not sure how to do that.
我已将其加载到电子邮件对象中
I have it loaded into a email object
import email
f = open(kwargs['opt_emailfile'])
msg = email.message_from_file(f)
f.close()
所以我可以得到
msg['To']
msg['From']
但我想要所有的标题
解决方案
感谢回答,这是我所做的
Here is what I did thanks to the answer
f = open(kwargs['opt_emailfile'])
msg = email.message_from_file(f)
f.close()
parser = email.parser.HeaderParser()
headers = parser.parsestr(msg.as_string())
for h in headers.items():
print h
推荐答案
使用 HeaderParser 或许:
from email.parser import HeaderParser
parser = HeaderParser()
h = parser.parsestr(email)
print h.keys()
我刚刚注意到您编辑了您的问题.您实际上可以在不使用 HeaderParser 的情况下从您所拥有的信息中获得相同的信息.例如headers.items() 将返回带有标题和相应值的 2 元组列表.
I just noticed you edited your question. You can actually get the same information from what you had without using HeaderParser. e.g. headers.items() will return list of 2-tuples with headers and corresponding values.
这篇关于在 python 中仅提取电子邮件标头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 python 中仅提取电子邮件标头
基础教程推荐
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
