繁体   English   中英

使用python查找时间戳数据中的每日模式

[英]Finding daily patterns in timestamps data with python

所以我想分析智能家居的数据库数据。 这就是我的数据样子:

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
...

基本上,要计算我所需的全部时间(在特定时间打开的灯的数量)除以特定时间的不同日期的数量。

该脚本从数据库中获取最短和最长时间,并计算这些时间之间的时间。

# 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()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM