Given string "5mins", I want to separate them something like [5, 'mins']. I have tried doing:
>>> re.findall(r"\d+|\D+", '5mins')
['5', 'mins']
which seems to do good but:
>>> def separate(string):
... return re.findall(r"\d+|\D+", string)
...
>>> print(separate('3hours'))
['3', 'hours']
>>> print(separate('7secs'))
['7', 'secs']
>>> print(separate('now'))
['now']
The problem with this method is I get ValueError if I have done assignment:
>>> number, unit = separate('now')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack
The all I here want is to get a list of number and unit. If there is no number then I should get an empty string, so that I can process the time further.
Additionally it's good if number is not a string, like above separate() function returns.