Python Forum
Substracting today's date from a date in column of dates to get an integer value
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Substracting today's date from a date in column of dates to get an integer value
#1
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)
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 123 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Date Time Series Help...Please spra8560 2 313 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Create dual folder on different path/drive based on the date agmoraojr 2 378 Jan-21-2024, 10:02 AM
Last Post: snippsat
  Python date format changes to date & time 1418 4 518 Jan-20-2024, 04:45 AM
Last Post: 1418
  i tried to install python for the first time today and pretty certain im being remote brianlj 2 505 Oct-03-2023, 11:15 AM
Last Post: snippsat
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 961 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 1,161 Jun-19-2023, 02:12 PM
Last Post: DosAtPython
  PDF properties doesn't show created or modified date Pedroski55 4 1,007 Jun-19-2023, 08:09 AM
Last Post: Pedroski55
  How should I run pip-date in python3? newbieAuggie2019 5 1,775 Mar-31-2023, 03:21 PM
Last Post: snippsat
  How to see the date of installation of python modules. newbieAuggie2019 4 1,466 Mar-31-2023, 12:40 PM
Last Post: newbieAuggie2019

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020