简体   繁体   中英

How can I create a python program that can determine whether an email is a spam or not?

I'm new to python, and I want to create a program that can determine whether an email is a spam or not based on three factors.

The subject (if it's empty, it's spam), the sender (I only want people whose email addresses end with ' .com, ' for example, otherwise it's spam), and the date (I only want emails on non-weekend days, otherwise it's spam).

I did the subject part, and it works successfully.

The code is attached below. But I need help with the sender and the date part.

import pandas as pd
ExcelFile = pd.read_excel(r'C:\Users\Email Table.xlsx')
Subject = pd.DataFrame(ExcelFile, columns=['Subject'])

def spam(Subject):
    df_multiindex = ExcelFile.set_index(['Subject'])
    n = len(df_multiindex)
    
    for x in range(n):
        if ((pd.isnull(ExcelFile.loc[x, 'Subject'])) == True):
            print("Spam")
        else:
            print("not spam")

spam(Subject)

You don't provide the format/type of your mail address, so this is just an idea. Check if the sender address ends with ".com":

if address.endswith(".com"):
    print("Spam")
else:
    print("not spam")

You also do not provide information on how the date is formatted. Given a unix timestamp, it'd work like this:

from datetime import datetime

ts = 1652734079
dt_object = datetime.fromtimestamp(ts)
# Check if weekday is saturday/sunday
if dt_object.weekday() in [5, 6]:
    print("Spam")
else:
    print("not spam")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM