我想统计过去7天outlook电子邮件中出现错误类型以及服务器名称的主题。我是Python编码新手。有人能帮我吗
例如:- 我的收件箱中有邮件,主题行如下:
因此,我希望为每台服务器(例如-CP-TEST-DB2、g-PROD-AUDB)获取主题行为“检查缺少的备份”的邮件,并希望从服务器角度对其进行计数。 比如我为“CP-TEST-DB2”服务器发送了多少封“检查缺少的备份”邮件。 对于每台服务器,我为“G-PROD-AUDB”等提供了多少“检查缺少的备份”邮件
对于“CP-TEST-DB2”服务器,我有多少封“检查死锁”邮件。 每台服务器有多少“G-PROD-AUDB”等“检查死锁”邮件。。。 对于错误类型也是如此。 我有8种类型的sql错误警报邮件,每33台服务器
import win32com.client
import imp, sys, os, re
import datetime as dt
import time
date_time = dt.datetime.now()
print (date_time)
#list of errors
error = ['There are errors in the SQL Error Log on', 'Check for missing backups on', 'Check drive space on', 'Check memory usage on', 'Check deadlock on ']
#list of server
server = ['TEST-AUDB','TEST-AUDB','EUDB','TEST-EUDB','PROD-AUDB','PROD-EUDB']
#setup range for outlook to search emails (so we don't go through the entire inbox)
lastHourDateTime = dt.datetime.now() - dt.timedelta(days = 7)
#print (lastHourDateTime)
outlook = win32com.client.Dispatch("Outlook.Application").GetNamespace("MAPI")
inbox = outlook.Folders.Item(2).Folders['Inbox']
messages = inbox.Items
messages.sort("[ReceivedTime]", True)
lastHourMessages = messages.Restrict("[ReceivedTime] >= '" +lastHourDateTime.strftime('%m/%d/%Y %H:%M %p')+"'")
print ("Current time: "+date_time.strftime('%m/%d/%Y %H:%M %p'))
for msg in lastHourMessages:
subject = msg.Subject
time = msg.ReceivedTime
print (s1)```
可以使用以下正则表达式:
它将匹配每个大写字母、数字和破折号中的一个或多个,直到句子结束。这涵盖了您在问题中提供的所有案例,如here所示
接下来,您可以使用
re
模块,遍历字符串列表,使用上面提到的模式搜索匹配项,并将信息存储在嵌套字典中希望这有帮助
相关问题 更多 >
编程相关推荐