在python中将时间字符串转换为秒

dam*_*mon 44 python time

我需要将以下格式给出的时间值字符串转换为秒.我正在使用 python2.6

例如:

1.'00:00:00,000'  -> 0 seconds

2.'00:00:10,000'  -> 10 seconds

3.'00:01:04,000' -> 64 seconds

4. '01:01:09,000' -> 3669 seconds
Run Code Online (Sandbox Code Playgroud)

我是否需要使用正则表达式来执行此操作?我尝试使用时间模块,但是 time.strptime('00:00:00,000','%I:%M:%S')扔了

ValueError: time data '00:00:00,000' does not match format '%I:%M:%S'
Run Code Online (Sandbox Code Playgroud)

谁能告诉我这是如何解决的?

编辑:

我认为

 pt =datetime.datetime.strptime(timestring,'%H:%M:%S,%f')
 total_seconds = pt.second+pt.minute*60+pt.hour*3600
Run Code Online (Sandbox Code Playgroud)

给出正确的值..我使用的是错误的模块

Bur*_*lid 57

对于Python 2.7:

>>> import datetime
>>> import time
>>> x = time.strptime('00:01:00,000'.split(',')[0],'%H:%M:%S')
>>> datetime.timedelta(hours=x.tm_hour,minutes=x.tm_min,seconds=x.tm_sec).total_seconds()
60.0
Run Code Online (Sandbox Code Playgroud)


use*_*943 30

我认为会有更多的pythonic方式:

timestr = '00:04:23'

ftr = [3600,60,1]

sum([a*b for a,b in zip(ftr, map(int,timestr.split(':')))])
Run Code Online (Sandbox Code Playgroud)

输出为263秒.

我很想知道是否有人可以进一步简化它.

  • 列表推导式更加Pythonic。所以 `sum([a*b for a,b in zip(ftr, [int(i) for i in timestr.split(":")])])` 会更Pythonic。 (2认同)
  • 谢谢Le Vieux Gildas..它应该是263..ftr不应该是[3600,60,0]..它必须是[3600,60,1]...谢谢agian (2认同)

tha*_*itz 18

没有进口

time = "01:34:11"
sum(x * int(t) for x, t in zip([3600, 60, 1], time.split(":"))) 
Run Code Online (Sandbox Code Playgroud)

  • 出色的答案,稍加调整即可正确处理mm:ss和hh:mm:ss格式,只需反转分割即可。`sum(x * int(t)for zip中的x,t([1,60,3600] ,reversed(time.split(“:”))))` (4认同)

Pio*_*otr 8

def time_to_sec(t):
   h, m, s = map(int, t.split(':'))
   return h * 3600 + m * 60 + s

t = '10:40:20'
time_to_sec(t)  # 38420
Run Code Online (Sandbox Code Playgroud)

  • 添加解释通常很有帮助 (4认同)

Mik*_*ton 6

看起来你愿意去掉几分之一秒......问题是你不能使用'00'作为小时 %I

>>> time.strptime('00:00:00,000'.split(',')[0],'%H:%M:%S')
time.struct_time(tm_year=1900, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=0, tm_yday=1, tm_isdst=-1)
>>>
Run Code Online (Sandbox Code Playgroud)


Nic*_*ood 5

总是有手工解析

>>> import re
>>> ts = ['00:00:00,000', '00:00:10,000', '00:01:04,000', '01:01:09,000']
>>> for t in ts:
...     times = map(int, re.split(r"[:,]", t))
...     print t, times[0]*3600+times[1]*60+times[2]+times[3]/1000.
... 
00:00:00,000 0.0
00:00:10,000 10.0
00:01:04,000 64.0
01:01:09,000 3669.0
>>> 
Run Code Online (Sandbox Code Playgroud)

  • 我讨厌用 Python 手工做事:p (3认同)

jfs*_*jfs 5

要获得timedelta(),您应该减去1900-01-01

>>> from datetime import datetime
>>> datetime.strptime('01:01:09,000', '%H:%M:%S,%f')
datetime.datetime(1900, 1, 1, 1, 1, 9)
>>> td = datetime.strptime('01:01:09,000', '%H:%M:%S,%f') - datetime(1900,1,1)
>>> td
datetime.timedelta(0, 3669)
>>> td.total_seconds() # 2.7+
3669.0
Run Code Online (Sandbox Code Playgroud)

%H 以上暗示输入少于一天,以支持时差超过一天:

>>> import re
>>> from datetime import timedelta
>>> td = timedelta(**dict(zip("hours minutes seconds milliseconds".split(),
...                           map(int, re.findall('\d+', '31:01:09,000')))))
>>> td
datetime.timedelta(1, 25269)
>>> td.total_seconds()
111669.0
Run Code Online (Sandbox Code Playgroud)

.total_seconds()在Python 2.6上进行仿真:

>>> from __future__ import division
>>> ((td.days * 86400 + td.seconds) * 10**6 + td.microseconds) / 10**6
111669.0
Run Code Online (Sandbox Code Playgroud)