简体   繁体   中英

How do I compare times using an if statement in python from a csv file

I am trying to build a program which reads dates from a csv file. I then wish to compare them using an if statement

However every time I attempt this in every way I have tried it results in a lot of errors

I get the time by slicing from the csv file where a column consists of both the time and date [Device Timestamp]

Dates_and_times = pd.Series(readings[Device Timestamp]

Reading_times = dates_and_times.str.slice(0, 10)

I then attempt the if statement to sort the reading times into categories

If 11:00:00 < reading_times <=16:00:00 Afternoon_reading+=1

Then I get hit with the following errors

Invalid syntax (:)

And a lot more at runtime including cannot convert between instances of str and int etc

Can someone please tell me how to put these times in a suitable format to be used in an if statement

Sample date in the csv: "05/11/2019 12:20:00"

Possible solution for possible format:

datetime.datetime.strptime('05/11/2019 12:20:00', '%d/%m/%Y %H:%M:%S')

The concept is to describe how your date looks using characters the the module datetime understands.

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