I am aware that with the timedelta function you can convert seconds to h:m:s using something like:
我知道使用timedelta函数你可以使用类似的东西将秒转换为h:m:s:
>> import datetime
>> str(datetime.timedelta(seconds=666))
'0:11:06'
But I need to convert h:m:s to seconds, or minutes.
但我需要将h:m:s转换为秒或分钟。
Do you know a function that can do this?
你知道一个可以做到这一点的功能吗?
6 个解决方案
#1
13
def hms_to_seconds(t):
h, m, s = [int(i) for i in t.split(':')]
return 3600*h + 60*m + s
#2
12
>>> import time, datetime
>>> a = time.strptime("00:11:06", "%H:%M:%S")
>>> datetime.timedelta(hours=a.tm_hour, minutes=a.tm_min, seconds=a.tm_sec).seconds
666
And here's a cheeky one liner if you're really intent on splitting over ":"
如果你真的打算分裂“:”,那么这是一个厚颜无耻的单行班轮
>>> s = "00:11:06"
>>> sum(int(i) * 60**index for index, i in enumerate(s.split(":")[::-1]))
666
#3
1
Unfortunately, it's not as trivial as constructing a datetime
object from a string using datetime.strptime
. This question has been asked previously on Stack Overflow here: How to construct a timedelta object from a simple string , where the solution involved using python-dateutil.
不幸的是,使用datetime.strptime从字符串构造datetime对象并不简单。之前在Stack Overflow上已经提出了这个问题:如何从一个简单的字符串构造一个timedelta对象,其中解决方案涉及使用python-dateutil。
Alternatively, if you don't want to have to add another module, here is a class you can use to parse a timedelta
from a string: http://kbyanc.blogspot.ca/2007/08/python-reconstructing-timedeltas-from.html
或者,如果您不想添加另一个模块,可以使用以下类来解析字符串中的timedelta:http://kbyanc.blogspot.ca/2007/08/python-reconstructing-timedeltas- from.html
#4
1
>>> def tt(a):
... b = a.split(':')
... return int(b[0]) * 3600 + int(b[1]) * 60 + int(b[2])
...
>>> print tt('0:11:06')
666
666
#5
0
This works in 2.6.4:
这适用于2.6.4:
hours, minutes, seconds = [int(_) for _ in thestring.split(':')]
If you want to turn it back into a timedelta:
如果你想把它变回timedelta:
thetimedelta = datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds)
#6
0
I'm not even sure I'd bother with timedelta for this
我甚至不确定我是否会为timedelta而烦恼
>>> pseconds = lambda hms:sum(map(lambda a,b: int(a)*b,hms.split(':'),(3600,60,1)))
>>> pseconds('0:11:06')
666
#1
13
def hms_to_seconds(t):
h, m, s = [int(i) for i in t.split(':')]
return 3600*h + 60*m + s
#2
12
>>> import time, datetime
>>> a = time.strptime("00:11:06", "%H:%M:%S")
>>> datetime.timedelta(hours=a.tm_hour, minutes=a.tm_min, seconds=a.tm_sec).seconds
666
And here's a cheeky one liner if you're really intent on splitting over ":"
如果你真的打算分裂“:”,那么这是一个厚颜无耻的单行班轮
>>> s = "00:11:06"
>>> sum(int(i) * 60**index for index, i in enumerate(s.split(":")[::-1]))
666
#3
1
Unfortunately, it's not as trivial as constructing a datetime
object from a string using datetime.strptime
. This question has been asked previously on Stack Overflow here: How to construct a timedelta object from a simple string , where the solution involved using python-dateutil.
不幸的是,使用datetime.strptime从字符串构造datetime对象并不简单。之前在Stack Overflow上已经提出了这个问题:如何从一个简单的字符串构造一个timedelta对象,其中解决方案涉及使用python-dateutil。
Alternatively, if you don't want to have to add another module, here is a class you can use to parse a timedelta
from a string: http://kbyanc.blogspot.ca/2007/08/python-reconstructing-timedeltas-from.html
或者,如果您不想添加另一个模块,可以使用以下类来解析字符串中的timedelta:http://kbyanc.blogspot.ca/2007/08/python-reconstructing-timedeltas- from.html
#4
1
>>> def tt(a):
... b = a.split(':')
... return int(b[0]) * 3600 + int(b[1]) * 60 + int(b[2])
...
>>> print tt('0:11:06')
666
666
#5
0
This works in 2.6.4:
这适用于2.6.4:
hours, minutes, seconds = [int(_) for _ in thestring.split(':')]
If you want to turn it back into a timedelta:
如果你想把它变回timedelta:
thetimedelta = datetime.timedelta(hours=hours, minutes=minutes, seconds=seconds)
#6
0
I'm not even sure I'd bother with timedelta for this
我甚至不确定我是否会为timedelta而烦恼
>>> pseconds = lambda hms:sum(map(lambda a,b: int(a)*b,hms.split(':'),(3600,60,1)))
>>> pseconds('0:11:06')
666