我试图从.txt文件(https://www.py4e.com/code3/mbox.txt)中提取包含多个电子邮件的唯一电子邮件地址列表 . 我可以通过以下程序将搜索范围缩小到'From:'和'To:'来提取电子邮件地址列表:
import re
in_file = open('dummy_text_file.txt')
for line in in_file:
if re.findall('^From:.+@([^\.]*)\.', line):
countFromEmail = countFromEmail + 1
print(line)
if re.findall('^To:.+@([^\.]*)\.', line):
print(line)
但是,由于各种电子邮件地址重复,这并没有为我提供唯一的列表 . 此外,最终打印的内容如下所示:
致:java-user@lucene.apache.org
来自:Adrien Grand jpountz@gmail.com
我期待只列出没有'to','from'或尖括号(<>)的实际电子邮件地址 .
我不熟悉Python,但我接近这个的原始方法是提取纯电子邮件地址,并可能将它们存储在某处并创建一个for循环以将它们添加到列表中 .
任何帮助或指向正确的方向将不胜感激 .
3 回答
要获取唯一的电子邮件列表,我会查看以下两篇文章:
https://www.peterbe.com/plog/uniqifiers-benchmark
How do you remove duplicates from a list whilst preserving order?
要将
Adrien Grand < jpountz@gmail.com >
解析为其他格式,以下链接应包含您需要的所有信息 .https://docs.python.org/3.7/library/email.parser.html#module-email.parser
不幸的是,我没时间给你写一个例子,但我希望这会有所帮助 .
最简单的方法是
set()
.集仅包含唯一值 .
Read more here for the unordered collection of unique elements (SET) in python
我已经为您编辑和评论了您的代码 . 希望这可以帮助 . 干杯! :)
-Sunjun