Expense reports
Report accepted values and rejected rows without hiding data-quality problems.
On this page
The concept
A data pipeline has distinct stages: read, validate, aggregate, and report. This example requires category and amount columns. It parses amounts as Decimal, requires finite nonnegative values, and records invalid row numbers instead of inventing a zero amount.
The function returns both totals and errors. That is a deliberate partial-import policy: valid rows contribute to totals, but callers must disclose rejected rows. A system requiring all-or-nothing imports would instead reject the batch. Choose that policy from requirements, not convenience.
Worked example
import csv
from decimal import Decimal, InvalidOperation
from io import StringIO
def summarize_expenses(text):
reader = csv.DictReader(StringIO(text))
if reader.fieldnames != ["category", "amount"]:
raise ValueError("expected category,amount header")
totals = {}
errors = []
for line_number, row in enumerate(reader, start=2):
try:
category = (row["category"] or "").strip()
amount = Decimal(row["amount"] or "")
if not category or not amount.is_finite() or amount < 0 or (None in row):
raise ValueError("invalid expense")
except (InvalidOperation, ValueError):
errors.append(f"line {line_number}: invalid expense")
continue
totals[category] = totals.get(category, Decimal("0")) + amount
return (totals, errors)
if __name__ == "__main__":
data = "category,amount\nbooks,12.50\nfood,5.20\nbooks,7.50\nfood,bad\n"
totals, errors = summarize_expenses(data)
for category, total in sorted(totals.items()):
print(f"{category}: {total:.2f}")
for error in errors:
print(error)
print("rejected rows:", len(errors))Trace the result
- The header check establishes the expected schema before processing rows.
- Each row receives a source line number and is validated before aggregation.
- The result shows totals and an explicit rejected-row count, keeping a partial report honest.
books: 20.00
food: 5.20
line 5: invalid expense
rejected rows: 1Line-by-line walkthrough
Line 1: import csv
Load csv (CSV quoting, headers, and row processing). Importing makes these names available; it is not a call to every function in the module.
Line 2: from decimal import Decimal, InvalidOperation
Import Decimal, InvalidOperation from decimal. This supplies explicit decimal arithmetic. The caller can now use the imported names directly.
Line 3: from io import StringIO
Import StringIO from io. This supplies in-memory stream objects. The caller can now use the imported names directly.
Lines 6–22: def summarize_expenses(text):
Define the function summarize_expenses with input names text. Definition stores the operation rather than executing its body. An ordinary call executes a normal function; a coroutine runs when awaited/scheduled, and a generator advances when consumed. Decorators can provide a context-manager or wrapper interface. Indented statements below belong to this operation.
Line 7: reader = csv.DictReader(StringIO(text))
Evaluate csv.DictReader(StringIO(text)), then bind or update reader with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.
Lines 8–9: if reader.fieldnames != ["category", "amount"]:
Evaluate reader.fieldnames != ['category', 'amount']. Execute the indented body if it is truthy; otherwise follow the alternative branch, if present. Branch order and indentation are part of the behavior.
Line 9: raise ValueError("expected category,amount header")
Raise ValueError('expected category,amount header'). Normal execution stops here unless an enclosing handler catches it. A from cause, when present, preserves the reason for a translated error.
Line 10: totals = {}
Evaluate {}, then bind or update totals with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Line 11: errors = []
Evaluate [], then bind or update errors with the result. The chosen collection determines how later access, membership, and mutation behave. Assignment itself does not imply a deep copy.
Lines 12–21: for line_number, row in enumerate(reader, start=2):
Obtain each item from enumerate(reader, start=2) and bind it to (line_number, row) before executing the body. A tuple-shaped target unpacks that item. A loop else, if present, runs only when completion does not use break.
Lines 13–20: try:
Run the protected body. A matching except handles its specified failure. An else suite runs after a successful try body, and finally runs when leaving the construct. This structure should handle expected failures without concealing unrelated bugs.
Line 14: category = (row["category"] or "").strip()
Evaluate (row['category'] or '').strip(), then bind or update category with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.
Line 15: amount = Decimal(row["amount"] or "")
Evaluate Decimal(row['amount'] or ''), then bind or update amount with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.
Lines 16–17: if not category or not amount.is_finite() or amount < 0 or (None in row):
Evaluate not category or not amount.is_finite() or amount < 0 or (None in row). Execute the indented body if it is truthy; otherwise follow the alternative branch, if present. Branch order and indentation are part of the behavior.
Line 17: raise ValueError("invalid expense")
Raise ValueError('invalid expense'). Normal execution stops here unless an enclosing handler catches it. A from cause, when present, preserves the reason for a translated error.
Handler for (InvalidOperation, ValueError): control reaches this suite only for a matching exception from the protected body.
Line 19: errors.append(f"line {line_number}: invalid expense")
Call errors.append to add one item to the existing list. This call may have an effect even when its return value is not assigned.
Line 20: continue
Skip the rest of this iteration and proceed to the next loop cycle; this does not terminate the loop.
Line 21: totals[category] = totals.get(category, Decimal("0")) + amount
Evaluate totals.get(category, Decimal('0')) + amount, then bind or update totals[category] with the result. Assignment itself does not imply a deep copy.
Line 22: return (totals, errors)
Evaluate (totals, errors) and return that value to the caller. This ends the current function call; subsequent statements on this execution path do not run.
Lines 25–32: if __name__ == "__main__":
Enter the program entry point only during direct execution. Importing this module still creates definitions but does not run this guarded workflow.
Line 26: data = "category,amount\nbooks,12.50\nfood,5.20\nbooks,7.50\nfood,bad\n"
Evaluate 'category,amount\nbooks,12.50\nfood,5.20\nbooks,7.50\nfood,bad\n', then bind or update data with the result. This literal is prepared example data. Assignment itself does not imply a deep copy.
Line 27: totals, errors = summarize_expenses(data)
Evaluate summarize_expenses(data), then bind or update (totals, errors) with the result. The call finishes before its return value is bound; a failed call does not produce a successful assignment. Assignment itself does not imply a deep copy.
Lines 28–29: for category, total in sorted(totals.items()):
Obtain each item from sorted(totals.items()) and bind it to (category, total) before executing the body. A tuple-shaped target unpacks that item. A loop else, if present, runs only when completion does not use break.
Line 29: print(f"{category}: {total:.2f}")
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Lines 30–31: for error in errors:
Obtain each item from errors and bind it to error before executing the body. A tuple-shaped target unpacks that item. A loop else, if present, runs only when completion does not use break.
Line 31: print(error)
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Line 32: print("rejected rows:", len(errors))
Evaluate the print arguments from the current state and display them. sep controls separators and end controls the ending when those options are provided. Displaying a value does not return that value from the surrounding function.
Common mistake
Silently replacing malformed amounts with zero produces misleading reports. Decimal also requires an explicit currency and rounding policy in real financial systems.