Home > Net >  How to find the exact values in csv file and execute another function?
How to find the exact values in csv file and execute another function?

Time:07-07

My python script reads csv file and if Date in csv file matches with server date(Today date), then it executes an function(probably read the name in sample)

My csv sample file Date:

Date,Name
"16,30,7",Sparrow
"1,30",Eagle
"6,13",Penguin

My python script :

user_inp="C:/MasterData/Userinputcsv"
with open(user_inp,'r') as f:
 user_inp=DictReader(f)
 for row in user_inp:
  if str(date.today().day) in row['Date']:
    print(row['Name'])


My expected Output : Penguin
My output : Sparrow,Penguin

Since 6 was in 16 ,it consider that.I dont need this.If 6 was date,then 16,26 should not be outcome.Request any idea,help on this

CodePudding user response:

This is one option. Change the date string to a list of integers and then check if date.today().day is in it.

user_inp="C:/MasterData/Userinputcsv"
with open(user_inp,'r') as f:
    dr = DictReader(f)
    for row in dr:
        date_list = [int(x) for x in row["Date"].split(",")]
        if date.today().day in date_list:
            print(row['Name'])

When run, I get:

Penguin
  • Related