red
red

Reputation: 1

How to sum date time from a CSV

I am trying to add up the time difference from a CSV file and print it in the console. What do I have to do for it?

The CSV file looks like this:

16:21,06:15,10:06
22:00,23:00,01:00
20:20,21:00,00:40
20:00,22:20,01:20

My Code so far:

import csv

with open("time.csv") as timedata:
   total = 0
   for row in csv.reader(timedata):
   total += 

Thanks for your help

Upvotes: 0

Views: 169

Answers (1)

tryexcept0
tryexcept0

Reputation: 158

I think it should solve your problem.

import csv

def toSeconds(t):
    return int(t.split(":")[0]) * 60 + int(t.split(":")[1])

def toTime(seconds):
    minutes = seconds // 60
    seconds = seconds % 60
    return str(minutes) + ":" + str(seconds)


with open("time.csv") as timedata:
    total = 0
    for row in csv.reader(timedata):
        for col in row:
            total += toSeconds(col)
    total = toTime(total)

Upvotes: 1

Related Questions