Apr-20-2019, 05:37 PM
It's also worth mentioning that if there is other invalid data in the file, regex may be his only option. It wouldn't be too hard to add to your code, @snippsat.
It's literally just the import and 1 additional line:
It's literally just the import and 1 additional line:
import re validNumPattern = re.compile(r"\A\d+\Z") with open('numbers.txt') as f: for line in f: line = line.strip() if validNumPattern.search(line): print(int(line))It can't hurt to be thorough...