Subtract date time from current datetime of csv file in python -
i trying calculate time difference in minutes when file created , updated until current moment calculate number of missing data. unable subtract them properly. code follows.
the data in file looks this:
date,temp1 2017-08-14 17:10:01,0.44 2017-08-14 17:15:01,0.62 2017-08-14 17:20:01,0.99 2017-08-14 17:25:01,0.85 2017-08-14 17:30:01,0.13 2017-08-14 17:35:01,0.24 2017-08-14 17:40:02,0.95 2017-08-14 17:45:01,0.65
and code is:
#!/usr/bin/env python # -*-coding:utf-8 -* import os import sys import time time import mktime, strftime, localtime, sleep datetime import datetime, timedelta def missing_data(expect, missed): . . . . def main(): fmt = '%y-%m-%d %h:%m:%s' open('/home/kamil/desktop/sensor1/temp.dat', 'rb') f: read = f.readlines()[1:] line in read: line = line.strip().split(',') # print line start_date = line[0] # type str convert datetime start_date = datetime.strptime(start_date, fmt) # type datetime.datetime , here need first timestamp in file current_time = datetime.now() step_size = 5 differ_time = current_time - start_date minutes_values = differ_time.total_seconds() / 60 print 'minutes_values:::::::::::::::::::', minutes_values missed = int(minutes_values / step_size) expected = ......... missing_data(expected, missed) if __name__ == '__main__': main()
and if print minutes_values, gives me results this:
40.755 35.7553 30.755 25.755 20.755 15.755 10.755 5.755 0.755
but require last value e.g 40.755 in case. new coding. can me in regard how obtain 1 single value.
first, create list save minutes_values, use builtin max() largest value.
def main(): fmt = '%y-%m-%d %h:%m:%s' open('/home/kamil/desktop/sensor1/temp.dat', 'rb') f: read = f.readlines()[1:] minutes_values = [] line in read: line = line.strip().split(',') # print line start_date = line[0] # type str convert datetime start_date = datetime.strptime(start_date, fmt) # type datetime.datetime , here need first timestamp in file current_time = datetime.now() step_size = 5 differ_time = current_time - start_date minutes_values.append( differ_time.total_seconds() / 60 ) missed = int(minutes_values / step_size) expected = ......... missing_data(expected, missed) print 'minutes_values:::::::::::::::::::', max(minutes_values)
Comments
Post a Comment