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
rust
use std::collections::HashMap; #[derive(Debug)] pub struct RequestHead {
Parsing an HTTP request head in Rust
parsing
error-handling
iterators
Intermediate
9 steps
php
<?php final class MarkdownParser {
Building a small Markdown-to-HTML parser in PHP
state-machine
parsing
closures
Intermediate
10 steps
python
from itertools import product from dataclasses import dataclass from decimal import Decimal
Generating product variants with itertools.product
cartesian-product
dataclass
decimal
Intermediate
7 steps
python
from typing import Callable, Dict, Type class PluginRegistry:
A decorator-based plugin registry in Python
decorators
registry pattern
factory
Intermediate
9 steps
go
package humanize import ( "fmt"
Parsing human-readable byte sizes in Go
parsing
regex
lookup-table
Intermediate
8 steps
python
from fastapi import APIRouter, BackgroundTasks, Depends, HTTPException, status from pydantic import BaseModel, EmailStr from sqlalchemy.orm import Session
Building a signup endpoint in FastAPI
dependency-injection
request-validation
background-tasks
Intermediate
8 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.