Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare time in Python 2

I get a .csv file with values inside and one of the columns contains durations in the format hh:mm:ss for example 06:42:13 (6 hours, 42 minutes and 13 seconds). Now I want to compare this time with a given time for example 00:00:00 because I have to handle the information in that row different.

time is the value I got out of the .csv file

if time == 00:00:00:
  do something
else:
  do something different

Thats what I want but it obviously doesn't work how I did it. I thought python stored the time as a String but when i compared it like this:

if time == "00:00:00":

it didn't work either.

Thats how I get the values out of the .csv file:

import csv
import_list = []
with open("input.csv", "r") as csvfile:
    inputreader = csv.reader(csvfile, delimiter=';')
    for row in inputreader:
        import_list.append(row)

The .csv file looks like this:

Name; Duration; Tests; Warnings; Errors
Test1; 06:42:13; 2000; 2; 1
Test2; 00:00:00; 0; 0; 0

and so on.

like image 977
L4st0n3 Avatar asked Jun 06 '26 14:06

L4st0n3


1 Answers

Try it like this:

if time == " 00:00:00":
    ...

You have a trailing space at the beginning. Alternatively you can change your code into this:

import csv
import_list = []
with open("input.csv", "r") as csvfile:
    inputreader = csv.reader(csvfile, delimiter=';')
    for row in inputreader:
        import_list.append([item.strip() for item in row])
like image 114
zipa Avatar answered Jun 09 '26 04:06

zipa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!