Python Forum

Full Version: Substracting today's date from a date in column of dates to get an integer value
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello to All!
I'm new in python programming and i need your help with the following problem. I have a CSV file with a column containing multiple dates. I want to use a loop (which ever works best) to iterate over the column of containing the dates, while at the same time during the iteration subtract today's date such if the difference between a date in the column and today's date equal a certain integer then it will print that date in the column that make produce that integer value. Please find the code snippet below. Thanks.
#Load all necessary packages and modules
import csv
import datetime

with open(r'myCSVpath', 'r') as csvFile:
    csv_file_reader = csv.DictReader(csvFile)
    for row in csv_file_reader:
        for row in csv_file_reader:
            #date2 = datetime.datetime.strptime(date2, "%Y-%m-%d").date()
            now = datetime.datetime.now()
            date2 = datetime.datetime.strptime(date2, "%Y-%m-%d").date()
            if (row['date'] = date2):
                if (date2 - now).days == 2:
                    print(date2)
better use 'Insert python',
and you have no indention

for row in csv_file_reader: is doubled


#Load all necessary packages and modules
import csv
import datetime

with open(r'myCSVpath', 'r') as csvFile:
    csv_file_reader = csv.DictReader(csvFile)
    for row in csv_file_reader:
        #date2 = datetime.datetime.strptime(date2, "%Y-%m-%d").date()
        now = datetime.datetime.now()
        date2 = datetime.datetime.strptime(date2, "%Y-%m-%d").date()
        if (row['date'] = date2):
            print(date2)
edit: maybe you have edited when I wrote this.