如何从CSV计算日期时间总和

bxpogfeg  于 2022-12-15  发布在  其他
关注(0)|答案(1)|浏览(122)

我想把csv文件中的时差加起来,然后在控制台中打印出来。我该怎么做呢?
csv文件如下所示:
16时21分06时15分10时06分
22点23点01点
20点20分21点40分
20点22点20分01点20分
我目前的代码:

import csv

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

谢谢你的帮忙

798qvoo8

798qvoo81#

我想这应该能解决你的问题。

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)

相关问题