python添加时间戳_在python中添加时间戳

这两个解决方案(AFAIK)都可以在python的任何2.x版本上运行(因此保证了相当多的向后兼容性)

仅依赖于regex库的实现: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))

这里有一个没有依赖关系的实现:

^{pr2}$

你可能感兴趣的:(python添加时间戳)