简体   繁体   中英

Finding daily patterns in timestamps data with python

So i want to analyze database data of smart house. This is how data i have look like:

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

Basically to calculate all i need is divide (number of light turns on at certain hour) by number of different dates at certain hour.

This script takes lowest and maximum time from database and calculate time between these times.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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