I am aware that with the timedelta function you can convert seconds to h:m:s using something like:
>> import datetime
>> str(datetime.timedelta(seconds=666))
'0:11:06'
But I need to convert h:m:s to seconds, or minutes.
Do you know a function that can do this?
I'm not even sure I'd bother with timedelta for this
And here's a cheeky one liner if you're really intent on splitting over ":"
Unfortunately, it's not as trivial as constructing a
datetime
object from a string usingdatetime.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.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.htmlThis works in 2.6.4:
If you want to turn it back into a timedelta:
666