如何使用python搜索主题行中有字符串的outlook电子邮件?

2024-09-29 19:20:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我想统计过去7天outlook电子邮件中出现错误类型以及服务器名称的主题。我是Python编码新手。有人能帮我吗

例如:- 我的收件箱中有邮件,主题行如下:

  1. 检查CP-TEST-DB2上是否缺少备份
  2. 检查G-PROD-AUDB上的死锁
  3. LF-PTT-DW1上的SQL错误日志中存在错误
  4. 检查CP-TEST-DB1上的驱动器空间

因此,我希望为每台服务器(例如-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)```





Tags: testimport服务器timeon错误dt邮件
1条回答
网友
1楼 · 发布于 2024-09-29 19:20:47

可以使用以下正则表达式:

([\-A-Z0-9]+)$

它将匹配每个大写字母、数字和破折号中的一个或多个,直到句子结束。这涵盖了您在问题中提供的所有案例,如here所示

接下来,您可以使用re模块,遍历字符串列表,使用上面提到的模式搜索匹配项,并将信息存储在嵌套字典中

import re

# Example strings
strings = ["Check for missing backups on CP-TEST-DB2",
            "Check deadlock on CP-TEST-DB2",
            "Check deadlock on CP-TEST-DB2",
            "Check deadlock on G-PROD-AUDB",
            "There are errors in the SQL Error Log on LF-PTT-DW1",
            "Check drive space on CP-TEST-DB1"]

# Declare empty dictionary
occurrences = {}

# Iterate over all the examples
for string in strings:
    results = re.search('([\-A-Z0-9]+)$', string)
    # Get the server from the regex match
    server = results.group(0)
    # Remove the server from the string and use strip to get rid of the trailing whitespace
    instance = string.replace(server, '').strip()
    # If the server is still not in the dict, insert it manually
    if occurrences.get(server, None) is None:
        occurrences[server] = {instance: 1}
    # If the server is already in the dict, but not the instance, create the key and initial value for the instance
    elif occurrences[server].get(instance, None) is None:
        occurrences[server][instance] = 1
    # Otherwise, just increment the value for the server-instance pair
    else:
        occurrences[server].update({instance : occurrences[server].get(instance, 0) + 1})
print(occurrences)

希望这有帮助

相关问题 更多 >

    热门问题