我在CSV文件中有以下类型的数据:
1; 28.11.2014 3:2:43;
0; 28.11.2014 3:3:58;
1; 28.11.2014 5:31:36;
0; 28.11.2014 5:33:7;
2; 28.11.2014 7:10:40;
0; 28.11.2014 9:50:10;
...
我要做的是计算每个状态标志的启动时间,然后对这些数量求和。在例子中;标志1是向上75秒(第1行)等。首先计算行之间的所有时间(然后在某处),然后总结时间或以其他方式计算它是否最有效?
答案 0 :(得分:0)
import datetime
import csv
totalTime = datetime.timedelta(0)
with open('path/to/file') as infile:
reader = csv.reader(infile, delimiter=';')
for start, end in zip(reader):
start = start[1]
end = end[1]
start = datetime.datetime.strptime(start, "%d.%m.%Y %H:%M:%S")
end = datetime.datetime.strptime(end, "%d.%m.%Y %H:%M:%S")
totalTime += end-start
print("total time flags were up", totalTime.total_seconds())