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>Just group them in a list and output whatever you want:</p>

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

with open("b.csv") as f:
    has_birthday = [user for user, birthday in csv.reader(f) if birthday == today]
    print(has_birthday)
</code></pre>

<p>Output:</p>

<pre><code>['user1', 'user3']
</code></pre>

<p>You can add a custom message based on the length of has_birthday:</p>

<pre><code>has_birthday = [user for user, birthday in csv.reader(f) if birthday == today]
frm = "{} have their birthdays today." if len(has_birthday) &gt; 1 else "{} has their birthday today."
print(frm.format(" and ".join(has_birthday or ["Nobody"])))
</code></pre>

<p>So for one matching birthday:</p>

<pre><code>userx has their birthday today.
</code></pre>

<p>For more than one:</p>

<pre><code>userx and Usery have their birthdays today.
</code></pre>

<p>And for no user:</p>

<pre><code>Nobody has their birthday today.
</code></pre>

python date csv datetime smtp