Read and analyze a text file
examples/files/text_report.txt
This is a text report there are some lines that start with Report: 23 Other linese has this somewhere in the middle. Begin report Report: -3 Like this. Report: 17 More lines starting with Report: 44 End report We will have some exercise with this file. Maybe 4 exercises. Report: 123
examples/files/read_report.py
import sys if len(sys.argv) != 2: exit(f"Usage: {sys.argv[0]} FILENAME") filename = sys.argv[1] total = 0 with open(filename, "r") as fh: for row in fh: if "Report" not in row: continue text, value = row.split(":") # print(value) value = float(value.strip()) # print(value) total += value print(total)