Add timestamps in python -
how add following timestamps month wise in python 2.4 i.e, timestamps occurring in month 3 should added together.
for example: result should on 2011-03 total timestamp 1:00:45 , on other months..
2011-03-07 0:27:41 2011-03-06 0:13:41 2011-03-08 0:17:40 2011-03-04 0:55:40 2011-05-16 0:55:40 2011-05-18 0:55:40 2011-07-16 0:55:40 2011-07-17 0:55:40
how this:
import datetime import re collections import defaultdict months = defaultdict(int) # months = {} # python 2.4 open("test.txt") timestamps: line in timestamps: month = line[:7] time = re.search(r"(\d+):(\d+):(\d+)", line) if time: seconds = int(time.group(1))*3600 + \ int(time.group(2))*60 + \ int(time.group(3)) months[month] += seconds # if month in months: # python 2.4 # months[month] += seconds # else: # months[month] = seconds month in sorted(months.keys()): print("times {}: {}".format(month, datetime.timedelta(seconds=months[month])))
output:
times 2011-03: 1:54:42 times 2011-05: 1:51:20 times 2011-07: 1:51:20
Comments
Post a Comment