Q:

Python Converting String Hours Minutes to Minutes

So say I have a list like so:
runtimes = ['24 min per ep',
     '1 hr 55 min',
     '24 min per ep',
     '25 min per ep',
     '23 min per ep',
     '23 min per ep',
     '23 min per ep',
     '27 min per ep',
     '24 min per ep',
     '23 min per ep',
     '24 min',
     '22 min per ep',
     '25 min per ep',
     '24 min per ep',
     '23 min per ep',
     '24 min per ep',
     '24 min per ep',
     '24 min per ep',
     '24 min per ep',
     '1 hr 41 min',
     '1 hr 27 min',
     '25 min per ep',
     '1 hr 22 min',
     '30 min per ep',
     '25 min per ep',
     '1 hr 31 min',
     '2 hr 4 min',
     '24 min per ep',
     '24 min per ep']

I want to convert these values into purely minutes:
I first thought it would be best to use two regex expressions like the ones below to replace the "1 hr" with "60 min" and then remove all the non-numerics, but this does not seem ideal.
re.sub("1 hr", "60 min", runtimes))

re.sub("\D", "", runtimes)

If someone knows a better way to do this it would be greatly appreciated.
Please let me know if you have any more questions.
Thank you for your help.

A:

This is fairly simple just to code without using any special kind of parser:
def parse_runtime(runtime):
    mins = 0
    fields = runtime.split()
    for idx in range(0, len(fields)-2):
        if fields[idx+1] in ('min', 'mins', 'minutes'):
            mins += int(fields[idx])
        elif fields[idx+1] in ('hr', 'hrs', 'hours'):
            mins += int(fields[idx]) * 60

    return mins

runtime_mins = []
for runtime in runtimes:
    try:
        mins = parse_runtime(runtime)
        runtime_mins.append(mins)
    except ValueError:
        print('Bad runtime: ' + runtime)

print(runtime_mins)

