【问题标题】:What is the best way to count time durations in CSV file?在 CSV 文件中计算持续时间的最佳方法是什么?
【发布时间】:2014-12-17 08:14:12
【问题描述】:
我在 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 行)等。首先计算行之间的所有时间(然后放在某个地方)然后总结时间或其他方式是否最有效?
【问题讨论】:
标签:
python
datetime
csv
row
【解决方案1】:
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())