Python Forum

Full Version: Python time
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,

I need to create a program that converts an input like this one:

1m and 45s
10m,10s
32s, and 12h
76h
1s

Into an output like this one:

00:01:45
00:10:10
12:00:32
76:00:00
00:00:01



Does anyone have an idea of how to do this in a file.read?



Thanks in advance
since your input can vary a lot but all have in common that you either have the m, s or h part, you could use regex to filter them out.
for example lets say you have following line in your document "10m,10s"
import re
print(re.findall("[0-9]?[0-9][m,s,h]", "10m,10s"))
Output:
["10m", "10s"]
From there your could build your desired output :)

P.S.: The regex currently only works if all numbers are lower than 100