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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A table of (name, start, end) offsets keeps fixed-width parsing declarative and easy to adjust.
- 2Yielding records lazily lets callers stream through large reports without loading everything into memory.
- 3Wrapping conversions in a try/except that adds the line number turns cryptic errors into actionable ones.
Related explainers
python
from fastapi import FastAPI, WebSocket, WebSocketDisconnect app = FastAPI()
Building a WebSocket chat with FastAPI
websockets
broadcast
connection-management
Intermediate
9 steps
ruby
class UserAgentParser BROWSERS = [ [/Edg\/([\d.]+)/, "Edge"], [/OPR\/([\d.]+)/, "Opera"],
Parsing user-agent strings in Ruby
regex
pattern-matching
lookup-tables
Intermediate
8 steps
python
import time import uuid from django.utils.deprecation import MiddlewareMixin
Attaching per-request context in Django
middleware
request lifecycle
multi-tenancy
Intermediate
7 steps
javascript
function evaluate(expression) { const tokens = tokenize(expression); let pos = 0;
Building a recursive descent calculator
parsing
recursion
operator-precedence
Intermediate
8 steps
python
import random from typing import Iterator, List
How reservoir sampling picks k items
reservoir-sampling
streaming
randomness
Intermediate
5 steps
rust
use chrono::{Duration, NaiveDate}; #[derive(Debug)] pub struct DateRange {
Parsing and iterating date ranges in Rust
error-handling
iterators
parsing
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/parsing-fixed-width-records-in-python-explained-python-1adb/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.