为表字典赋值

2024-05-18 20:54:01 发布

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

我遇到过这样一种情况:销售经理会自动回复一封电子邮件,其中包含主要和次要潜在客户的交付周期。他们的回答将包括它是哪种类型的铅和铅的时间指示。例如:

Primary_Lead 10
Secondary_Lead 20

我写了一个脚本来浏览电子邮件(基于某个主题),并在回复中找到提前期。我想把这些加到交货期表字典里。我认为除了我的最后两行将值附加到Sales ManagerLead Time之外,一切都正常。我做错什么了?我知道整个电子邮件增加了额外的复杂程度,我不需要帮助,但我想提供实际的代码。你知道吗

import win32com.client
import re

olFolderInbox = 6
olMailItem = 0

outlook = win32com.client.Dispatch("Outlook.Application")
mapi = outlook.GetNamespace('MAPI')
inbox =  mapi.GetDefaultFolder(olFolderInbox)
My_Folder = inbox.Folders("Leads")


Lead_Time = {
    'Primary_Leads':         {'Sales Manager' : [],'Lead Time' : []},
    'Secondary_Leads':       {'Sales Manager' : [],'Lead Time' : []}

}

for i in range (My_Folder.Items.Count): #loops through all emails in Leads Folder
  message = My_Folder.Items[i] #sets the message
  if "RE: Lead Time Report" in message.Subject: #checks for a certain subject
    for tbl in Lead_Time:
        if tbl.upper() in message.Body.upper():
          tbl['Sales Manager'].append map(int,re.findall(tbl +"* (\d+)",message.Body,re.I))
          tbl['Lead Time'].append message.sender

Tags: inremessagefortime电子邮件mymanager
1条回答
网友
1楼 · 发布于 2024-05-18 20:54:01

当您真的想要附加到值时,您正在迭代Lead_Time的键。你知道吗

您可以在自己的代码中看到这种混淆:

for tbl in Lead_Time:
    if tbl.upper() in message.Body.upper():
        tbl['Sales Manager'].append

在第二行中,您将tbl视为字符串。在第三行,您将tbl视为字典中与其关联的数组值。你知道吗

您可以按以下方式更改代码:

Lead_Time[tbl]['Sales Manager'].append ...
Lead_Time[tbl]['Lead Time'].append ...

或者您可以在迭代时同时请求键和值:

for table_name, value in Lead_Time.items():
    if table_name.upper() in message.Body.upper():
        value['Sales Manager'].append ...

您可以通过以下更简单的示例看到默认dict迭代器的输出:

some_dict = {1: 2, 3: 4}
for key in some_dict:
    print key

打印1和3。你知道吗

相关问题 更多 >

    热门问题