0

I have some strings similar to the following:

"1h 43m"
"4d 21h"
"59m 55s"

How would I go about converting these times to the total seconds as an int/float?

For example:

"1h 43s" ----> 3643
"4d 21h" ---->  421200
"59m 55s" ----> 3595

Any advice would be greatly appreciated.

  • 1
    You can use a regular expression to parse the strings. Then you can easily calculate the total seconds from that. – Barmar Feb 03 '22 at 22:17
  • 2
    if you were feeling especially malicious, you could swap all the `h` and `m` for their multiplication equivalent, space for addition, and `eval()` the result – ti7 Feb 03 '22 at 22:19
  • The more complicated part is parsing the string. After that you can use builtin modules like `datetime` and use `timedelta` object with the `total_seconds` method. – MYousefi Feb 03 '22 at 22:32

0 Answers0