Match Current date with CSV file and print the matches
<p>hi so im writing a python script to send a birthday mail kind of thing . But im stuck in middle . i Have a csv file containing names and there birthdays and already wrote a code to get the current date ,</p>

<pre><code>#Import Date
import datetime
CurrentDate = datetime.datetime.now().date()
CurrentDate = CurrentDate.strftime("%d-%B-%Y")
print(CurrentDate)
</code></pre>

<p>and my csv file is </p>

<pre><code>user1,13-September-2016
user2,19-October-2016
user3,13-September-2016
user4,25-August-2016
</code></pre>

<p>So what i want is match current date with the second column of this csv and Get the corresponding column one value and export them as a string like i got the CurrentDate from datetime . If there's more than one user havind birthday on corresponding day an "and" should print between their names . I Hope i didn't make any mistake asking the question :)</p>

<p>Thank you everyone , So after doing some work and after getting all your helps , i was able to resolve this </p>

<pre><code>import csv
from datetime import datetime
today = datetime.now().date().strftime("%d-%B-%Y")

while True:
   with open("InputFile.csv") as d:
    has_birthday = [user for user, birthday, gender in csv.reader(d,skipinitialspace=True) if birthday == today]
   if len(has_birthday) &lt; 1:
    exit ()
   else:
     with open("InputFile.csv") as f:

       has_birthday = [user for user, birthday, gender in csv.reader(f,skipinitialspace=True) if birthday == today]

     with open("InputFile.csv") as g:

       male = [user for user, birthday, gender in csv.reader(g, delimiter=',', skipinitialspace=True) if birthday == today and gender == 'M']

     with open("InputFile.csv") as h:

       female = [user for user, birthday, gender in csv.reader(h, delimiter=',', skipinitialspace=True) if birthday == today and gender == 'F']

     if len(has_birthday) == 1 and male:
       frm = "{} has his birthday today."
     elif len(has_birthday) == 1 and female:
       frm = "{} has her birthday today."
     else:
       frm = "{} have their birthdays today."

     test = (frm.format(" and ".join([", ".join(has_birthday[:-1]), has_birthday[-1]]))) if len(has_birthday) &gt; 1 else frm.format(" and ".join(has_birthday))
     print test
</code></pre>

python date csv datetime smtp