python讀取eml文件并用正則表達式匹配郵箱的代碼
今天接到一個需求有一個同事離職了,但是留下了非常多(2W多封)的郵件,我需要將他的郵件進行分類,只要郵件中以@xxx.com結尾的存放在文件夾中(下圖名叫【是】的文件夾),否則放在另一個文件夾中(下圖名叫【否】的文件夾)。 目錄結構

代碼注意事項
import email(我發(fā)現(xiàn)是內置模塊,不用安裝) 下面是注意事項(就當是注釋吧!?。?!) 1、提取包含一下后綴的郵箱,我用了split(“@”),所以不用寫 @e_a = [‘Honeywell.com’, ‘honeywell.com’, ‘garrettmotion.com’, ‘HONEYWELL.COM’, ‘resideo.com’]
2、提取,收件人、發(fā)件人、抄送人的郵箱(這個是可以不寫的,但是我這個代碼是借鑒的,沒找到提取全部內容的函數(shù),只找到提取內容的函數(shù),所以加上了下面的代碼)fjr = email.utils.parseaddr(msg.get(“from”))[1]
3、將eml文件內容與收件人、發(fā)件人、抄送人拼接,并且加 " " 間隔,不加會有些小問題
sjr = email.utils.parseaddr(msg.get(‘to’))[1]
csr = email.utils.parseaddr(msg.get(‘cc’))[1]
print(“發(fā)件人”, fjr)
print(“收件人”, sjr)
print(“抄送人”, csr)text = text + " " + fjr + " " + " " + " " + " " + sjr + " " + " " + csr
4、正則匹配郵箱prog = re.compile(r’[a-zA-Z0-9_.±]+@[a-zA-Z0-9-]+.[a-zA-Z0-9-.]+')
5、移動文件 os.remove()
res = prog.findall(text)
完整代碼
import email
import os
import re
from email import policy
from email.parser import BytesParser
e_a = ['Honeywell.com', 'honeywell.com', 'garrettmotion.com', 'HONEYWELL.COM', 'resideo.com']
for f in os.listdir("./數(shù)據(jù)源/"):
# print(f)
text = ""
with open("./數(shù)據(jù)源/" + f, 'rb') as fp:
msg = BytesParser(policy=policy.default).parse(fp)
fjr = email.utils.parseaddr(msg.get("from"))[1]
sjr = email.utils.parseaddr(msg.get('to'))[1]
csr = email.utils.parseaddr(msg.get('cc'))[1]
print("發(fā)件人", fjr)
print("收件人", sjr)
print("抄送人", csr)
if msg.get_body(preferencelist=('plain'))==None:
text = text + " " + fjr + " " + " " + " " + " " + sjr + " " + " " + csr
else:
text = msg.get_body(preferencelist=('plain')).get_content()
text = text + " " + fjr + " " + " " + " " + " " + sjr + " " + " " + csr
# print(text)
prog = re.compile(r'[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+')
res = prog.findall(text)
for e in res:
res1 = e.split("@")[1]
if res1 in e_a:
print(f, "在")
ori = "./數(shù)據(jù)源/" + f
now = "./是/" + f
os.rename(ori, now)
break
else:
ori = "./數(shù)據(jù)源/" + f
now = "./否/" + f
os.rename(ori, now)
print(f, "不在")下面看看python正則表達式匹配郵箱
下面來看看python驗證郵箱模式的例子。
(首先還是把環(huán)境列出來)
環(huán)境:python 2.7.10
1. 一次匹配多個郵箱的情況
下面的例子中:郵箱中可以出現(xiàn) 數(shù)字、大小寫字母、下劃線、和橫線(-)
# -*- coding:utf-8 -*-
# 郵箱格式-正則表達式匹配
import re
# 一次匹配多個郵箱
str1 = 'aaf ssa@ss.net asdf asdb@163.com.cn asdf ss-a@ss.net asdf asdd.cba@163.com afdsaf'
reg_str1 = r'([\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+)'
mod = re.compile(reg_str1)
items = mod.findall(str1)
for item in items:
print item結果輸出:

2. 一次匹配一個
這種情況,常見在登錄界面用戶名為郵箱時, 此時一個字符串只有一個 郵箱
# 只匹配一個
str2 = 'ssa_a-c@ss.net.cn'
reg_str2 = r'(^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$)'
mod = re.compile(reg_str2)
items = mod.findall(str2)
for item in items:
print item結果輸出:

到此這篇關于python讀取eml文件并用正則匹配郵箱的文章就介紹到這了,更多相關python讀取eml文件內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Python-apply(lambda x: )的使用及說明
這篇文章主要介紹了Python-apply(lambda x: )的使用及說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Python數(shù)據(jù)分析模塊pandas用法詳解
這篇文章主要介紹了Python數(shù)據(jù)分析模塊pandas用法,結合實例形式詳細分析了Python數(shù)據(jù)分析模塊pandas的功能、常見用法及相關操作注意事項,需要的朋友可以參考下2019-09-09
pytorch 實現(xiàn)計算 kl散度 F.kl_div()
這篇文章主要介紹了pytorch 實現(xiàn)計算 kl散度 F.kl_div(),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-05-05

