How to convert a time string to seconds? How to convert a time string to seconds? python python

How to convert a time string to seconds?


For 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


A little more pythonic way I think would be:

timestr = '00:04:23'ftr = [3600,60,1]sum([a*b for a,b in zip(ftr, map(int,timestr.split(':')))])

Output is 263Sec.

I would be interested to see if anyone could simplify it further.


without imports

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