因此,我想对智能家居的数据库数据进行分析。这是我拥有的数据的样子:
ID NAME STATUS TIME
1 light 1 2016-06-25 08:00:00
2 light 1 2016-06-25 08:01:05
3 light 1 2016-06-25 08:00:21
4 light 1 2016-06-25 08:00:30
...基本上,我需要计算的是除以(在某个小时打开的灯的数量)除以在某个小时的不同日期的数量。
发布于 2016-08-26 01:47:14
此脚本从数据库获取最短时间和最长时间,并计算这两个时间之间的时间。
# db.txt
1 light 1 2016-06-25 08:00:00
1 light 1 2016-06-25 08:01:05
1 light 1 2016-06-25 08:00:21
1 light 1 2016-06-25 08:00:30
# python script
import datetime
def data():
with open('db.txt', 'r') as f:
for line in f.readlines():
row = line.split()
if row[2] == '1':
yield row[4]
data = sorted(data())
early = datetime.datetime.strptime(data[0], '%H:%M:%S')
lately = datetime.datetime.strptime(data[1], '%H:%M:%S')
sth_between = (lately - early)/2
print (early + sth_between).time()https://stackoverflow.com/questions/39151482
复制相似问题