Python - 使用 continue 语句跳过迭代,但仍在写入文件
Python - using continue statement to skip iteration, however still writing to file
我正在使用 Python 和 O365 Microsoft Office 365 Python API 软件包。
这是包裹的 link: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 时应该跳过当前迭代。
注:关键字A在blacklisted_keywordsA列表中,关键字B在blacklisted_keywordsB列表中也是如此。
出于某种原因,它没有跳过迭代并仍然将我不感兴趣的电子邮件写入文件,即使它包含列入黑名单的关键字,这种问题的可能解决方案是什么?
问题是 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)
我正在使用 Python 和 O365 Microsoft Office 365 Python API 软件包。 这是包裹的 link: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 时应该跳过当前迭代。 注:关键字A在blacklisted_keywordsA列表中,关键字B在blacklisted_keywordsB列表中也是如此。
出于某种原因,它没有跳过迭代并仍然将我不感兴趣的电子邮件写入文件,即使它包含列入黑名单的关键字,这种问题的可能解决方案是什么?
问题是 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)