Home > Software engineering >  Time and date strings to DateTime Objects from csv file in python
Time and date strings to DateTime Objects from csv file in python

Time:10-06

So I'm working on a function which checks the dates in each row of a csv file against a standard date made up of two cells in the header row. What I need to do is take the date from A2 and the time from A3 and concatenate them into one object which can be compared against the rest of the rows of the file and then from there expel the rows which fail the test.

The only problem I'm having is in running the comparison with the time objects and getting the strings out of the csv. My current code gives me a ValueError because the format of value of date_time does not match the format %m/%d/%Y %H:%M:%S. Which is correct, because the value of date_time is the whole entire line.

Right now I'm simply trying to get the comparison to run on an arbitrary static time.

But if I want to take the date from cell A2 and concatenate it with the time in cell A3, then compare that new object with the rest of the rows in the file whose time and date do not need concatenation, what is the best way to go about running this comparison when you don't know what the dates are going to be?

def CheckDates(f):
    with open(f, newline='', encoding='utf-8') as g:
        r = csv.reader(g)
        date_time = str(next(r))
        for line in r:
            if datetime.strptime(date_time, '%m/%d/%Y %H:%M:%S') >= datetime.strptime('01/11/2022 13:19:00', '%m/%d/%Y %H:%M:%S'):
                # Dates pass
                pass
            else:
                # Dates fail
                pass

edited typos and added an example csv

TD,08/24/2021,14:14:08,21012,223,0,1098,0,031,810,12,01,092,048,0008,02
Date/Time,G120010,M129000,G110100,M119030,G112070,G112080,G111030,G127020,G127030,G120020,G120030,G121020,G111040,G112010,P102000,G112020,G112040,G112090,G110050,G110060,G110070,T111100
06/27/2022 00:00:01,40,133.2,0,0,7.284853,0,0.6030464,0,0,1,0,5,11,5,0,0,414,344,0,154,0,5
06/27/2022 00:00:03,40,133.2,0,0,7.284853,0,0.5898247,0,0,1,0,5,11,5,0,0,414,344,0,154,0,5
06/27/2022 00:00:05,40,133.2,0,0,7.284853,0,0.6135368,0,0,1,0,5,11,5,0,0,414,344,0,154,0,5
06/27/2022 00:00:07,40,133.2,0,0,7.284853,0,0.6087456,0,0,1,0,5,11,5,0,0,414,344,0,154,0,5
06/27/2022 00:00:09,40,133.2,0,0,7.284853,0,0.5903625,0,0,1,0,5,11,5,0,0,414,344,0,154,0,5
06/27/2022 00:00:11,40,133.2,0,0,7.284853,0,0.5799789,0,0,1,0,5,11,5,0,0,414,344,0,154,0,5
06/27/2022 00:00:13,40,133.2,0,0,7.284853,0,0.5821953,0,0,1,0,5,11,5,0,0,414,344,0,154,0,5
06/27/2022 00:00:15,40,133.2,0,0,7.284853,0,0.6024017,0,0,1,0,5,11,5,0,0,414,344,0,154,0,5
06/27/2022 00:00:17,40,133.2,0,0,7.284853,0,0.5984001,0,0,1,0,5,11,5,0,0,414,344,0,154,0,5

CodePudding user response:

This should do the trick. I modified a couple rows of your data file for "dramatic effect"...

# time compare

from datetime import datetime, timedelta

f = 'data.csv'

with open(f, 'r') as src:
    row_0 = src.readline()
    tokens = row_0.strip().split(',') # split (tokenize) the line 
    orig_time = tokens[1]   ' '   tokens[2]   # concatenate the strings
    base_time = datetime.strptime(orig_time, '%m/%d/%Y %H:%M:%S')
    print(f'recovered this base time: {base_time}')
    
    src.readline()  # burn row 2

    # process the remainder
    for line in src:
        tokens = line.strip().split(',')
        row_time = datetime.strptime(tokens[0], '%m/%d/%Y %H:%M:%S')

        # calculate the difference.  The result of comparing datetimes
        # is a "timedelta" object that can be queried.
        td = row_time - base_time

        # make a comparision to see if it is pos/neg
        if td < timedelta(0):
            print('this line is before the base time:')
            print(f'    {line}')

Output:

recovered this base time: 2021-08-24 14:14:08
this line is before the base time:
    06/27/2019 00:55:05,40,133.2,0,0,7.284853,0,0.6135368,0,0,1,0,5,11,5,0,0,414,344,0,154,0,5

this line is before the base time:
    06/27/2021 10:00:11,40,133.2,0,0,7.284853,0,0.5799789,0,0,1,0,5,11,5,0,0,414,344,0,154,0,5

this line is before the base time:
    06/27/2020 00:00:17,40,133.2,0,0,7.284853,0,0.5984001,0,0,1,0,5,11,5,0,0,414,344,0,154,0,5
  • Related