如何在python 2.4中逐月添加以下时间戳,即第3个月中发生的所有时间戳应该加在一起。
例如:结果应该像2011-03总时间戳是1:00:45,依此类推其他月份..
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
答案 0 :(得分:2)
这个怎么样:
import datetime
import re
from collections import defaultdict
months = defaultdict(int)
# months = {} # for Python 2.4
with open("test.txt") as timestamps:
for 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
for month in sorted(months.keys()):
print("Times for {}: {}".format(month,
datetime.timedelta(seconds=months[month])))
输出:
Times for 2011-03: 1:54:42
Times for 2011-05: 1:51:20
Times for 2011-07: 1:51:20
答案 1 :(得分:1)
假设数据已经排序,您可以使用itertools.groupby:
import datetime as dt
import itertools as it
data='''\
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
'''.splitlines()
dates=[dt.datetime.strptime(line,'%Y-%m-%d %X') for line in data]
for key,group in it.groupby(dates,lambda d: (d.year,d.month)):
seconds=sum(date.hour*3600+date.minute*60+date.second for date in group)
print('{k[0]}-{k[1]:02d} {d}'.format(
k=key,
d=dt.timedelta(seconds=seconds)))
产量
2011-03 1:54:42
2011-05 1:51:20
2011-07 1:51:20
并且(当然)如果数据尚未排序,那么您可以在使用dates.sort()
之前使用itertools.groupby
对其进行排序。
答案 2 :(得分:1)
这两个解决方案--AFAIK - 都应该适用于任何2.x版本的python(从而保证了一些向后兼容性)
仅依赖于正则表达式库的实现:
import re
data = '''
2011-03-07 0:27:41
2011-03-06 0:13:41
2011-03-05 0:17:40
2011-03-04 0:55:40
2011-05-16 0:55:40
2011-05-16 0:55:40
2011-07-16 0:55:40
2011-07-16 0:55:40
'''
def group(month):
li = re.findall(r'2011-%s-\d\d (\d+:\d+:\d+)' % str(month).zfill(2), data)
seconds = 0
for log in li:
log = [int(n) for n in log.split(':')]
seconds += log[0]*3600 + log[1]*60 + log[2]
hours = seconds / 3600
seconds -= 3600*hours
minutes = seconds / 60
seconds -= 60*minutes
return "%02d:%02d:%02d" % (hours, minutes, seconds)
for month in range(1,13):
print "In month %d you worked %s" % (month, group(month))
这里的实施没有依赖:
def group(month):
to_find = '2011-%s-' % str(month).zfill(2)
logs = []
for line in data.split('\n'):
point = line.find(to_find)
if point != -1:
logs.append(line[-8:])
seconds = 0
for log in logs:
log = [int(n) for n in log.split(':')]
seconds += log[0]*3600 + log[1]*60 + log[2]
hours = seconds / 3600
seconds -= 3600*hours
minutes = seconds / 60
seconds -= 60*minutes
return "%02d:%02d:%02d" % (hours, minutes, seconds)