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

问题描述 投票:0回答:1

我想计算最近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”邮件有“检查死锁”邮件...对于错误类型也是如此。我每33个服务器有8种类型的sql错误警报邮件?

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)```




python regex python-3.x outlook win32com
1个回答
0
投票

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

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

将匹配每个大写字母,数字和破折号中的1个或多个,直到句子结尾。如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)

希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.