通过for循环中的字典中的列表迭代

发布于 2025-01-26 07:15:31 字数 1330 浏览 1 评论 0 原文

我正在尝试将Outlook中的所有邮件存储在数据库中。为此,我需要在Outlook中迭代每个文件夹。我正在使用win32com.client。我创建了一个词典,将邮箱的每个名称作为键,所有文件夹都作为带有值的列表。

postbox_and_folders = {} 
folder_of_postbox = [] 
for postbox in postboxes:
    for idx, folder in enumerate(mapi.Folders(postbox).Folders):
        folder_of_postbox.append(str(folder))
        postbox_and_folders[postbox] = folder_of_postbox
        if str(folder) == 'Archive':
            folder_of_postbox = [] 
print(postbox_and_folders)

输出看起来像这样:

{'@VPC': ['Calendar', 'Contacts', 'Conversation Action Settings', 'Conversation History', 'Deleted Items', 'Drafts', 'Einstellungen für QuickSteps', 'ExternalContacts', 'Files', 'Inbox', 'Journal', 'Junk Email', 'Notes', 'Outbox', 'PersonMetadata', 'Sent Items', 'Social Activity Notifications', 'Sync Issues', 'Tasks', 'Yammer Root', 'Archive'], '@FCC': ['Calendar', 'Contacts', 'Conversation Action Settings', 'Conversation History', ...] which is exactly how it should look.

现在是我的目标,即通过每个邮箱及其各自的文件夹来存储消息正文中的数据库中。

我知道我必须使用mapi.folders,但无法使其与此词典一起使用。 我如何使用此词典迭代每个文件夹?

我只需要将词典放在此功能中,我觉得我非常接近它。

for key, value in postbox_and_folders.items():
messages = mapi.Folders(str(key)).Folders(value[i]).Items
for message in list(messages)[:10]:
    print(message.Body)

I am trying to store all mails from outlook in a database. for that i need to iterate through every folder i have in outlook. I am using win32com.client for that. I created a dictionary with every name of the postbox as a key and all of the folders as a list with values.

postbox_and_folders = {} 
folder_of_postbox = [] 
for postbox in postboxes:
    for idx, folder in enumerate(mapi.Folders(postbox).Folders):
        folder_of_postbox.append(str(folder))
        postbox_and_folders[postbox] = folder_of_postbox
        if str(folder) == 'Archive':
            folder_of_postbox = [] 
print(postbox_and_folders)

The output looks like this:

{'@VPC': ['Calendar', 'Contacts', 'Conversation Action Settings', 'Conversation History', 'Deleted Items', 'Drafts', 'Einstellungen für QuickSteps', 'ExternalContacts', 'Files', 'Inbox', 'Journal', 'Junk Email', 'Notes', 'Outbox', 'PersonMetadata', 'Sent Items', 'Social Activity Notifications', 'Sync Issues', 'Tasks', 'Yammer Root', 'Archive'], '@FCC': ['Calendar', 'Contacts', 'Conversation Action Settings', 'Conversation History', ...] which is exactly how it should look.

Now is my goal to go through each postbox and their respective folders to store the body of the messages in a database.

I know I have to use mapi.Folders but am not able to make it work with this dictionary.
How do I iterate through every folder with this dictionary?

I just have to put the dictionary in this function and I feel like I'm pretty close to it.

for key, value in postbox_and_folders.items():
messages = mapi.Folders(str(key)).Folders(value[i]).Items
for message in list(messages)[:10]:
    print(message.Body)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神回复 2025-02-02 07:15:31

您需要递归迭代所有子文件夹。例如,C#中的原始草图是所有类型的编程语言中常见的Outlook对象模型:

private void EnumerateFoldersInDefaultStore()
{
    Outlook.Folder root =
        Application.Session.
        DefaultStore.GetRootFolder() as Outlook.Folder;
    EnumerateFolders(root);
}

// Uses recursion to enumerate Outlook subfolders.
private void EnumerateFolders(Outlook.Folder folder)
{
    Outlook.Folders childFolders =
        folder.Folders;
    if (childFolders.Count > 0)
    {
        foreach (Outlook.Folder childFolder in childFolders)
        {
            // Write the folder path.
            Debug.WriteLine(childFolder.FolderPath);
            // Call EnumerateFolders using childFolder.
            EnumerateFolders(childFolder);
        }
    }
}             

您可能会找到类似的线程有用。

You need to iterate over all subfolders recursively. For example, a raw sketch in C# as the Outlook object model is common for all kind of programming languages:

private void EnumerateFoldersInDefaultStore()
{
    Outlook.Folder root =
        Application.Session.
        DefaultStore.GetRootFolder() as Outlook.Folder;
    EnumerateFolders(root);
}

// Uses recursion to enumerate Outlook subfolders.
private void EnumerateFolders(Outlook.Folder folder)
{
    Outlook.Folders childFolders =
        folder.Folders;
    if (childFolders.Count > 0)
    {
        foreach (Outlook.Folder childFolder in childFolders)
        {
            // Write the folder path.
            Debug.WriteLine(childFolder.FolderPath);
            // Call EnumerateFolders using childFolder.
            EnumerateFolders(childFolder);
        }
    }
}             

You may find a similar Problem with loop all the emails inside the outlook folder and sub-folder thread helpful.

本宫微胖 2025-02-02 07:15:31

如果要存储文件夹名称,则(或者)也可以存储文件夹条目ID( mapifolder.entryid )。如果您知道文件夹条目ID,则可以随时使用 application.session.getFolderFromid 打开它。

但是请记住,您只需处理一个级别的文件夹,您确实需要递归处理它们 - 创建一个将 mapifolder 作为参数的函数。然后,该函数可以通过子文件夹( mapifolder.folders Collection)枚举,并为每个子文件夹呼叫。

If you are storing the folder names, you might as well (or instead) store the folder entry ids (MAPIFolder.EntryID). If you know the folder entry id, you can open it at any moment using Application.Session.GetFolderFromID.

Keep in mind however that you only process one level of folders, and you really need to process them recursively - create a function that takes MAPIFolder as a parameter. The function can then enumerate through the child folders (MAPIFolder.Folders collection) and call itself for each subfolder.

骷髅 2025-02-02 07:15:31

您错过了一个循环,可以通过每个邮箱中的文件夹迭代,请尝试一下,

for key, value in postbox_and_folders.items():
    for i in value:    
        messages = mapi.Folders(str(key)).Folders(value[i]).Items
        for message in list(messages)[:10]:
            print(message.Body)

希望这有效!

You have missed a for loop to iterate through the folders in each postbox, try this,

for key, value in postbox_and_folders.items():
    for i in value:    
        messages = mapi.Folders(str(key)).Folders(value[i]).Items
        for message in list(messages)[:10]:
            print(message.Body)

Hope this works!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文