【问题标题】:Python - using continue statement to skip iteration, however still writing to filePython - 使用 continue 语句跳过迭代,但仍写入文件
【发布时间】:2021-07-07 13:20:08
【问题描述】:

我正在使用 Python 和 O365,即 Microsoft Office 365 Python API 包。 这是包的链接:https://github.com/O365/python-o365

我正在编写一个脚本来自动发送具有某些属性的电子邮件,并跳过具有我不感兴趣的其他属性的电子邮件。

我过滤了所有电子邮件,只产生具有特定电子邮件主题的邮件,现在我正在进一步过滤它们。

这是我的代码:

from O365 import Account, FileSystemTokenBackend, message, connection, MSGraphProtocol

import datetime
import traceback
import logging

todays_date = datetime.datetime.now()
todays_day = todays_date.day

#Accessing mailbox

mailbox = account.mailbox("myemail@email.com")

inbox = mailbox.inbox_folder()
junk_folder = mailbox.junk_folder()
messages_retrieved_from_inbox = inbox.get_messages()
messages_retrieved_from_junkfolder = junk_folder.get_messages(limit= 30, download_attachments= True)



#Taking care of messages
blacklisted_keywordsA = ['abc', 'def', 'ghi']
blacklisted_keywordsB = ['sometext', 'someothertext']

with open("myFile.txt", "a+", encoding="UTF-8") as my_file:
    for message in messages_retrieved_from_junkfolder:
       if(message.subject == "Some Subject" and message.created.day == todays_day):
           print("Found Email with that subject that I am looking for!")
           message_body = message.get_body_text()
           for keywordA in blacklisted_keywordsA:
               if(keywordA in message_body):
                   print("BlackListed keywordA ! Skip this inquiry.")
                   continue
           
           for keywordB in blacklisted_keywordsB:
               if(keywordB in message_body):
                   print("Blacklisted keywordB! Skip this inquiry.")
                   continue
    
           my_file.write("My Message:\n")
           my_file.write(message_body)

        

       else:
           print("Not Interested in this Email!")

continue 语句应该在找到 message_body 中的关键字 A 时跳过当前迭代。 注意:keywordA 在 blacklisted_keywordsA 列表中,KeywordB 在 blacklisted_keywordsB 列表中。

由于某种原因,它并没有跳过迭代,仍然将我不感兴趣的电子邮件写入文件,即使它包含列入黑名单的关键字,这种问题的可能解决方案是什么?

【问题讨论】:

    标签: python outlook office365


    【解决方案1】:

    问题是continue 将继续最里面的循环。在这段代码中:

    for keywordB in blacklisted_keywordsB:
      if(keywordB in message_body):
        print("Blacklisted keywordB! Skip this inquiry.")
        continue
    

    continue 语句将继续 for keywordB in.... 循环,而不是外部循环。见:https://docs.python.org/3/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops

    您有一个选择是设置一个标志,在您进行写入时检查该标志。像这样的:

    skip_message = False
    for keywordB in blacklisted_keywordsB:
      if(keywordB in message_body):
        print("Blacklisted keywordB! Skip this inquiry.")
        skip_message = True
        break #Break out of the keyword loop
    
    if not skip_message:
      my_file.write("My Message:\n")
      my_file.write(message_body)
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-31
      • 1970-01-01
      相关资源
      最近更新 更多