st = "08:30:30"
et = "9:33:33"
#方法一
def t2s(t):
h,m,s = t.strip().split(":")
return int(h) * 3600 + int(m) * 60 + int(s)
print(t2s(st))
#方法二
import datetime
var = ("hours","minutes","seconds")
time2sec = lambda x:int(datetime.timedelta(**{k:int(v) for k,v in zip(var,x.strip().split(":"))}).total_seconds())
print(time2sec(st))
stackoverflow.com上还有更多的写法,有兴趣可以自己去看。当然方法一最简单明了,直接用这样的方法是最好的。
http://stackoverflow.com/questions/10663720/converting-a-time-string-to-seconds-in-python
http://stackoverflow.com/questions/6402812/how-to-convert-an-hmmss-time-string-to-seconds-in-python
下面的方法是从stackoverflow上抄过来的。
http://stackoverflow.com/questions/775049/python-time-seconds-to-hms
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print ("%02d:%02d:%02d" % (h, m, s))
参考链接:
https://www.cnblogs.com/gayhub/p/6154707.html