Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Read and analyze a text file

{% embed include file="src/examples/files/text_report.txt)

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)