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
Question [SOLVED] [Windows] Change directory date? Winfried 2 1,415 May-11-2025, 04:24 PM
Last Post: Winfried
Exclamation Date format convert problem for SQL server eotret 5 3,991 Aug-09-2024, 07:19 AM
Last Post: Pedroski55
  Invalid Date Format fo Cached Files jland47 1 1,589 May-22-2024, 07:04 PM
Last Post: deanhystad
  Compare current date on calendar with date format file name Fioravanti 1 3,281 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Date Time Series Help...Please spra8560 2 2,107 Feb-01-2024, 01:38 PM
Last Post: spra8560
  Create dual folder on different path/drive based on the date agmoraojr 2 1,910 Jan-21-2024, 10:02 AM
Last Post: snippsat
  Python date format changes to date & time 1418 4 4,287 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 2,796 Oct-03-2023, 11:15 AM
Last Post: snippsat
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 3,915 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  Formatting a date time string read from a csv file DosAtPython 5 6,973 Jun-19-2023, 02:12 PM
Last Post: DosAtPython

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.