-1
def seconds_to_label_converter(seconds):
    hours = divmod(seconds,3600)[0]
    minutes = divmod(seconds-(hours*3600),60)[0]
    remaining_seconds = seconds-((hours*3600)+(minutes*60))
    if remaining_seconds == 0 and hours == 0 and minutes == 0:
        time_label = "No info"
    elif hours > 1: 
        time_label = f"{hours} Hours {minutes} Minutes {remaining_seconds} Seconds"
    elif hours == 1:
        time_label = f"{hours} Hour {minutes} Minutes {remaining_seconds} Seconds"
    elif hours == 0 and minutes > 1:
        time_label = f"{minutes} Minutes {remaining_seconds} Seconds"
    elif hours == 0 and minutes == 1:
        time_label = f"{minutes} Minute {remaining_seconds} Seconds"
    elif hours == 0 and minutes == 0:
        time_label = f"{remaining_seconds} Seconds"
    print(time_label)

seconds_to_label_converter(21254)

I have a "seconds to label converter" like this. Now I need a function that will do the opposite. But I don't know how to do it.

for example:

label_to_seconds_converter("5 Hours 54 Minutes 14 Seconds")
# >>> OUTPUT = 21254

4 Answers4

0

Try this. Extract specific strings using re and then extract numbers

import re
def seconds_to_label_converter(seconds):
    x=re.findall(r'\b\d+\s*hour[s]?\b',seconds.lower())
    y=re.findall(r'\b\d+\s*minute[s]?\b',seconds.lower())
    z=re.findall(r'\b\d+\s*second[s]?\b',seconds.lower())
    
    secs=0
    if x:
        for i in re.findall(r'\d+',''.join(x)):
            secs +=3600*int(i)
    if y:
        for i in re.findall(r'\d+',''.join(y)):
            secs +=60*int(i)
    if z:
        for i in re.findall(r'\d+',''.join(z)):
            secs +=int(i)
    return secs
        
print(seconds_to_label_converter('4 hours 50 seconds'))

Returns

14450

print(seconds_to_label_converter('4 hours 10 minutes 50 seconds'))

returns

15050

print(seconds_to_label_converter('5 Hours 54 Minutes 14 Seconds'))

Returns

21254
  • Everything works well with ur function except by this: ```print(seconds_to_label_converter('1 Minute 14 Seconds'))``` its returning 14. – Mehmet Güdük Aug 29 '21 at 09:31
0

You could do something like this:

def text_to_second(txt: str):
    items = [int(x) if x.isnumeric() else x for x in txt.split() ]
    seconds = 0
    for i in range(0,len(items),2):
        a = items[i+1]
        if a.lower()=="hours" or a.lower()=="hour":
            seconds += 3600*items[i]
        elif a.lower()=="minutes" or a.lower()=="minute":
            seconds += 60*items[i]
        elif a.lower()=="seconds" or a.lower()=="second":
            seconds += items[i]
    return seconds
    
print(text_to_second("5 Hours 54 Minutes 14 Seconds"))
#output: 21254
Pexicade
  • 31
  • 1
  • 5
0

try this :

def label_to_seconds(label):
    times = re.findall(r'\b\d+\s*[hours|minutes|seconds]?\b', label.lower())
    list_len = len(times)
    if list_len == 3:
        return int(times[0].strip()) * 3600 + int(times[0].strip()) * 60 + int(times[0].strip())
    if list_len == 2:
        return int(times[0].strip()) * 60 + int(times[1].strip())
    if list_len == 1:
        return int(times[0].strip())
    else:
        print('Invalid Format')
heretolearn
  • 6,387
  • 4
  • 30
  • 53
0

Another option would be to use the zip function:

def label_to_seconds_converter(label: str) -> int:
    label_list = label.split()
    seconds = 0
    for amount, word in zip(label_list[::2], label_list[1::2]):
        if "hour" in word.lower():
            seconds += int(amount) * 3600
        elif "minute" in word.lower():
            seconds += int(amount) * 60
        else:
            seconds += int(amount)
    return seconds


print(label_to_seconds_converter("5 Hours 54 Minutes 14 Seconds"))

result

21254
Edo Akse
  • 4,051
  • 2
  • 10
  • 21