我在 Python 中使用 Win32com 來訪問我的 Outlook 電子郵件,并且我正在使用正則運算式在我的電子郵件主題行中搜索案例編號。
例如,電子郵件主題行可能是 Hisenburg CASE# 0039484。我至少有 30 封這樣的電子郵件。
這是我的代碼:
import pandas as pd, os
from pandas import DataFrame as df
import win32com.client as client
import time, datetime, smtplib, imaplib, pathlib, re
outlook = client.Dispatch('Outlook.Application')
namespace = outlook.GetNameSpace('MAPI')
account = namespace.Folders['[email protected]']
inbox = account.Folders['Inbox']
ap = inbox.Folders['Dean Brown']
pattern = re.compile(r'CASE# \d\d\d\d\d\d\d')
for x in message.items:
b = re.findall(pattern, x.Subject)
c = []
for y in range(len(b)):
c.append(b[y])
print(c)
當我鍵入c以獲取串列中的專案時,出現以下錯誤: TypeError: 'NoneType' object is not subscriptable
如果我輸入c[1],我會收到以下錯誤:IndexError: list index out of range
當我的結果 (c) 填充 30 個數字時,為什么會收到此錯誤?有沒有辦法填充電子郵件的內容?我怎樣才能解決這個問題?
uj5u.com熱心網友回復:
您收到錯誤IndexError: list index out of range是因為您正在啟動一個回圈:
for y in range(len(b)):
然后,當您將第一個專案添加到陣列中時,您會直接在它之后詢問陣列中的第二個專案,使用c[1]它還不存在。
c.append(b[y])
print(c[1])
如果您想獲得第一項,您可以將縮進向左縮短一步,并根據串列中的可用結果使用c[0]或c[1]。
請注意,您不必執行回圈機制。
您可以直接使用re.findall回傳字串串列。
c = re.findall(r"CASE# \d{7}", x.Subject)
uj5u.com熱心網友回復:
我能夠找到解決方案。我的電子郵件中的內容沒有填充,我在 c.append(b[y]) 之后使用此代碼解決了問題:
for y in range(len(b)):
c.append(b[y])
with open('Case_File ' today '.txt', 'a') as Case_File:
Case_File.write('%s\n'%c)
這有助于將附加值發送到我能夠訪問并查看我所有案例編號的檔案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/372899.html
上一篇:為什么我的iOS應用無法使用Node.jsAgora令牌服務器創建的令牌通過AgoraRtcEngineKit進行身份驗證?
