Search code examples
pythonsmtplibsys

How to send email alert through python if a string is found in a csv file?


im new to python and trying to understand how to send email alerts. I have a csv file, the below is the dataframe for it:

,Latitude,Longtitude,LocationKey,MatchDescription
0,38.67694301003448,-77.7429029707531,609751,rooftop
1,29.372805012097956,-98.53188396363142,609750,rooftop
2,37.72961100923345,-122.38610096386918,609749,rooftop
3,42.509437395496974,-70.86884298867957,609748,rooftop
4,was not geocoded,rooftop
5,25.982770005233007,-80.16985001050395,609745,place

everyday this particular csv regenerates with new values, however, occasionally, I get the above error saying was not geocoded. I want a script that sends me an email alert each time that string shows up in this generated csv.

import pandas as pd
import smtplib
from email.message import EmailMessage

df = pd.read_csv('StgDimLocationEsri.csv')

cols_to_check = ["Latitude","Longtitude","LocationKey"]


def email_alert(subject,body,to):
            msg = EmailMessage()
            msg.set_content(body)
            msg['subject'] = subject
            msg['to'] = to
            # msg['from'] = from
            
            user = "[email protected]"
            msg['from'] = user
            password = "clqdgqyfrleisynd"
            
            server = smtplib.SMTP("smtp.gmail.com", 587)
            server.starttls()
            server.login(user,password)
            server.send_message(msg)
            
            server.quit()
if __name__ == '__main__':
    email_alert("Hey","Error","[email protected]")
    
for col in cols_to_check:
    if not pd.to_numeric(df[col], errors='coerce').all():
        email_alert()

The problem is that i am receiving an email even if there are only numerics under these columns. I tried to replace the was not geocoded with numericals and i still got the error email


Solution

  • Since you have a csv file, it is better to read it using pandas and then check whether all values are numeric in the specific columns, likewise :

    import pandas as pd
    import smtplib
    from email.message import EmailMessage
    
    df = pd.read_csv('StgDimLocationEsri.csv')
    
    cols_to_check = ["Latitude","Longtitude","LocationKey"]
    
    #df = pd.DataFrame({'Latitude': [11.324342,343.4552,434.3, 'nan',-34895.6661,1,2], 'Longtitude': list('ABCDEFG'), 'LocationKey': [38.676943448,-38.6769438,34,23,12,56,67]})
    
    def email_alert(subject,body,to):
        msg = EmailMessage()
        msg.set_content(body)
        msg['subject'] = subject
        msg['to'] = to
            
        user = "[email protected]"
        msg['from'] = user
        password = "pwd"
            
        server = smtplib.SMTP("smtp.gmail.com", 587)
        server.starttls()
        server.login(user,password)
        server.send_message(msg)
            
        server.quit()
    
    if __name__ == '__main__':
    
        for col in cols_to_check:
            if not df[col].apply(lambda x: isinstance(x, (int, float))).all():
            
            body = "There is a non-numeric value in column " + col + "."
         
            email_alert("Hey",body,"[email protected]")
    

    Since I did not have access to the csv file I tried creating a sample DataFrame which has been commented out in the above code.