Home > Mobile >  How to filter for a list of email subjects in outlook with python
How to filter for a list of email subjects in outlook with python

Time:12-14

i am working on a project to read emails that are using different email subjects in outlook. I have a list of email subjects, may I know how should I perform the coding to filter for a list of matches?

filtering I am referring to this documentation to restrict the filter, but there's no example on how to filter using a list instead of a string comparison.

can any masters here advice on this?

#example of the code
email_sub = ["Data File", "Confidential", "18-01 Excel", "Purchasing", "Purchase Trend"]

#set filter
Filter = "[Subject] = 'email_sub'"

CodePudding user response:

You can use Python's inbuilt filter function. Something like the following should work assuming you have a list of emails.

emails = []
email_sub = ["Data File", "Confidential", "18-01 Excel", "Purchasing", "Purchase Trend"]

def subject_filter(email):
    if email.subject in email_sub:
        return True
    else:
        return False

filtered_emails = filter(emails, subject_filter)

You can also do it more succinctly using list comprehension

[email for email in emails if email.subject in email_sub]

CodePudding user response:

Create a filter with multiple conditions connected with the Or operator:

Filter = "[Subject] = 'email_sub' Or [Subject] = 'email_sub 2'"
  • Related