import re def mextract(report, variable): try: result = re.search(variable + '(.*)\n', report) return(result.group(1).lstrip()) # lstrip removes leading spaces. except: return("[Variable Not found]")