#!/usr/bin/env python3 import re digits = ["zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"] group = "|".join(digits + ["[0-9]"]) tot = 0 with open("lines.txt") as lines: for line in lines.readlines(): print() line = line.rstrip() print(line) last = re.search(rf"^.*({group})", line) first = re.search(rf"({group}).*$", line) print(first, last) f = first[1] l = last[1] print(f, l) if f in digits: f = str(digits.index(f)) if l in digits: l = str(digits.index(l)) print(f, l) numb = int(f + l) tot += numb print(numb) print() print(tot)