在python中添加时间戳

在python中添加时间戳,python,Python,如何在Python2.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

如何在Python2.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
这个怎么样:

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
这个怎么样:

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

假设数据已经排序,您可以使用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

(当然)如果数据尚未排序,则在使用
itertools.groupby
之前,您可以使用
dates.sort()
对其进行排序假设数据已排序,则可以使用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

(当然)如果数据还没有排序,那么在使用
itertools.groupby
之前,您可以使用
dates.sort()
对其进行排序。这两种解决方案-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)
这里是一个没有依赖项的实现

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)

这两种解决方案——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)
这里是一个没有依赖项的实现

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)


你从哪里得到这些数据?它们是这种文本格式的吗?日期排序了吗?还有,我不能重现你的计算结果。所以第一列是日期,第二列是h:m:s在那个日期工作的时间?是否要汇总给定月份的工作小时数?日期按日期时间格式排序。是的,我要汇总给定月份的工作小时数请编辑时间戳。这是确切的数据您从哪里获得这些数据?它们是这种文本格式的吗?日期排序了吗?还有,我不能重现你的计算结果。所以第一列是日期,第二列是h:m:s在那个日期工作的时间?是否要汇总给定月份的工作小时数?日期按日期时间格式排序。是的,我要汇总给定月份的工作小时数请编辑时间戳。这是确切的数据Python 2.4没有defaultdict.rit。还有其他方法吗?为什么使用2.4?它有好几年了。(如果必须,只需使用普通的dict,并使用
.setdefault()
或在添加到条目之前检查该条目是否已经存在)。我们使用RHEL5,python是2.4v,甚至我们的服务器也是旧版本。python 2.4没有默认dict.rit。还有其他方法吗?为什么使用2.4?它有好几年了。(如果必须,只需使用普通的dict,并使用
.setdefault()
或在添加到条目之前检查该条目是否已经存在)。我们在其上使用RHEL5和python是2.4v,甚至我们的服务器也是过时的。对于其中的键,group.groupby(日期,lambda d:(d.tm_year(),d.tm_month())会出现错误:TypeError:“int”对象不可用callable@Rajeev:删除
d.tm_year()
d.tm_month()
中的括号。什么样的对象是
d
?什么是
tm_year
tm_month
…?我得到了一个关于键的错误,在其中分组。groupby(日期,lambda d:(d.tm_year(),d.tm_month()):TypeError:'int'对象不是callable@Rajeev:删除
d.tm_year()
d.tm_month()
中的括号。什么样的对象是
d
?什么是
tm\u年
tm\u月
。。。?