Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to parse a time

Tags:

python

Is there a way to parse the following Time without using something hacky like s.spilt() a bunch of times?

s = 'PT1H28M26S'

I would like to get:

num_mins = 88
like image 771
David542 Avatar asked May 11 '26 09:05

David542


1 Answers

You could use a regular expression:

>>> match = re.search(r"PT(\d+)H(\d+)M(\d+)S", s)
>>> h, m, s = map(int, match.groups())
>>> num_mins = h * 60 + m
>>> num_mins
88
like image 64
tobias_k Avatar answered May 12 '26 23:05

tobias_k