python 42 lines · 8 steps

Parsing fixed-width records in Python

A generator turns fixed-width text lines into typed Transaction records, slicing by column offsets and normalizing each field.

Explained by highlit
1import datetime
2from dataclasses import dataclass
3 
4 
5@dataclass
6class Transaction:
7 account_id: str
8 posted_on: datetime.date
9 description: str
10 amount: float
11 status: str
12 
13 
14_FIELDS = [
15 ("account_id", 0, 12),
16 ("posted_on", 12, 20),
17 ("description", 20, 60),
18 ("amount", 60, 74),
19 ("status", 74, 76),
20]
21 
22_STATUS_CODES = {"PO": "posted", "PN": "pending", "RV": "reversed"}
23 
24 
25def parse_report(lines):
26 for lineno, raw in enumerate(lines, start=1):
27 line = raw.rstrip("\n")
28 if not line.strip() or line.startswith("#"):
29 continue
30 
31 fields = {name: line[start:end].strip() for name, start, end in _FIELDS}
32 
33 try:
34 yield Transaction(
35 account_id=fields["account_id"],
36 posted_on=datetime.datetime.strptime(fields["posted_on"], "%Y%m%d").date(),
37 description=fields["description"],
38 amount=int(fields["amount"]) / 100.0,
39 status=_STATUS_CODES.get(fields["status"], "unknown"),
40 )
41 except ValueError as exc:
42 raise ValueError(f"malformed record on line {lineno}: {exc}") from exc
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1A table of (name, start, end) offsets keeps fixed-width parsing declarative and easy to adjust.
  2. 2Yielding records lazily lets callers stream through large reports without loading everything into memory.
  3. 3Wrapping conversions in a try/except that adds the line number turns cryptic errors into actionable ones.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Parsing fixed-width records in Python — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code