簡體   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