简体   繁体   中英

How to compare date from csv(string) to actual date

filenameA ="ApptA.csv" 
filenameAc = "CheckoutA.csv"

def checkouttenantA():
    global filenameA
    global filenameAc
    import csv
    import datetime
    with open(filenameA, 'r') as inp, open(filenameAc, 'a' , newline = "") as out:
        my_writer = csv.writer(out)
        for row in csv.reader(inp):
            my_date= datetime.date.today()
            string_date = my_date.strftime("%d/%m/%Y")
            if row[5] <= string_date:
                my_writer.writerow(row)

Dates are saved in format %d/%m/%Y in an excel file on column [5]. I am trying to compare dates in csv file with actual date, but it is only comparing the %d part. I assume it is because dates are in string format.

Ok so there are a few improvements to make as well, which I'll put as an edit to this, but you're converting todays date to a string with strftime() and comparing the two strings, you should be converting the string date from the csv file to a datetime object and comparing those instead.

I'll add plenty of comments to try and explain the code and the reasoning behind it.

# imports should go at the top
import csv

# notice we are importing datetime from datetime (we are importing the `datetime` type from the module datetime
import from datetime import datetime

# try to avoid globals where possible (they're not needed here)

def check_dates_in_csv(input_filepath):
    ''' function to load csv file and compare dates to todays date'''

    # create a list to store the rows which meet our criteria
    # appending the rows to this will make a list of lists (nested list)
    output_data = []

    # get todays date before loop to avoid calling now() every line
    # we only need this once and it'll slow the loop down calling it every row
    todays_date = datetime.now()

    # open your csv here using the function argument
    with open(input_filepath, output_filepath) as csv_file:
        reader = csv.reader(csv_file)

        # iterate over the rows and grab the date in each row
        for row in reader:
            string_date = row[5]

            # convert the string to a datetime object
            csv_date = datetime.strptime(string_date, '%d/%m/%Y')

            # compare the dates and append if it meets the criteria
            if csv_date <= todays_date:
                output_data.append(row)

         # function should only do one thing, compare the dates
         # save the output after
         return output_data

# then run the script here
# this comparison is basically the entry point of the python program
# this answer explains it better than I could: https://stackoverflow.com/questions/419163/what-does-if-name-main-do
if __name__ == "__main__":

    # use our new function to get the output data
    output_data = check_dates_in_csv("input_file.csv")

    # save the data here
    with open("output.csv", "w") as output_file:
        writer = csv.writer(output_file)
        writer.writerows(output_data)

I would recommend to use Pandas for such tasks:

import pandas as pd

filenameA ="ApptA.csv" 
filenameAc = "CheckoutA.csv"
today = pd.datetime.today()

df = pd.read_csv(filenameA, parse_dates=[5])
df.loc[df.iloc[:, 5] <= today].to_csv(filenameAc, index=False)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM