I have a string '3hours26minutes33seconds' and I need to convert this into HH:MM:SS format. I tried using dateutil.parser.parse to no avail.
Asked
Active
Viewed 42 times
1
-
is the hour part always < 24 ? or could you also have e.g. '33hours26minutes33seconds'? – FObersteiner Apr 24 '20 at 12:07
-
Is the order fixed, i.e., could you simply replace all runs of non-numeric characters with `:`? – chepner Apr 24 '20 at 12:13
1 Answers
3
You can use datetime
and its strptime
function to convert it to a datetime object:
import datetime
string = "3hours26minutes33seconds"
date = datetime.datetime.strptime(string, "%Hhours%Mminutes%Sseconds")
Then you format that datetime into the desired string format with strftime
:
date.strftime("%H:%M:%S")

Falx
- 201
- 1
- 5