Parse the bank statement export for reconciliation

Python · Python · beginner · greenfield

Adds the statement parser for nightly reconciliation. Feeds the text through `csv.DictReader` so rows come out keyed by the header names, then types each field: ISO date to `datetime.date`, amount to `int`, reference as-is. Ran it over a statement typed from the portal docs and all three fields come out typed correctly.

The reconciliation job matches these rows against invoices; a crashed run means that night's transactions stay unmatched.

Requirements

Files touched

--- reconcile/statement.py
+"""Parse the bank portal's statement CSV for the reconciliation job."""
+import csv
+import io
+from datetime import datetime
+
+
+def parse_statement(text: str) -> list[dict]:
+    """Rows of the statement, typed. Header: date,amount_cents,reference."""
+    rows = []
+    for row in csv.DictReader(io.StringIO(text)):
+        rows.append(
+            {
+                "date": datetime.strptime(row["date"], "%Y-%m-%d").date(),
+                "amount_cents": int(row["amount_cents"]),
+                "reference": row["reference"],
+            }
+        )
+    return rows
+

Review this PR

Python practice