Let's import a regex.
import re
Assume there's a string containing some data.
data = '''Mike: Jan 25.1, Feb 24.3, Mar 29.0
Rob: Jan 22.3, Feb 20.0, Mar 22.0
Nick: Jan 23.4, Feb 22.0, Mar 23.4'''
For example, we want to extract floats for Rob's line only.
name = 'Rob'
I'd make it like this:
def data_extractor(name, data):
return re.findall(r'\d+\.\d+', re.findall(r'{}.*'.format(name),data)[0])
The output is ['22.3', '20.0', '22.0'].
Is my way pythonic or it should be improved somehow? It does the job, but I'm not certain about appropriateness of such code.
Thanks for your time.