Parse Fixed Width Data File by Column Slices in Python

Extract fields from fixed-width text by slicing each line at defined column offsets, with a dictionary describing the boundaries.

Easy Python 3.9+ Aug 9, 2026 Files & data 13 views 0 copies

Python code

24 lines
Python 3.9+
from pathlib import Path


def parse_fixed_width(data: str, slices: dict[str, tuple[int, int]]) -> list[dict[str, str]]:
    lines = data.strip().splitlines()
    records = []
    for line in lines:
        record = {}
        for name, (start, end) in slices.items():
            record[name] = line[start:end].strip()
        records.append(record)
    return records


if __name__ == "__main__":
    data = "ALICE  12345ENGINEER\nBOB    67890DESIGNER\nCAROL  11223ANALYST "
    slices = {
        "name": (0, 7),
        "id": (7, 12),
        "role": (12, 20),
    }
    result = parse_fixed_width(data, slices)
    for rec in result:
        print(f"{rec['name']}: {rec['id']} ({rec['role']})")

Output

stdout
ALICE: 12345 (ENGINEER)
BOB: 67890 (DESIGNER)
CAROL: 11223 (ANALYST)

How it works

The parse_fixed_width function reads each line from the input string and applies Python string slicing using (start, end) offsets defined in the slices dictionary. Each slice is stripped of leading and trailing whitespace to clean up padded spaces. Because fixed-width files rely on consistent column positions, the offsets must match the physical layout of the data. The function returns a list of dictionaries, one per record, where keys come from the slice names and values are the extracted substrings.

Common mistakes

  • Using the wrong end index — slice `line[7:12]` includes index 11, not 12.
  • Forgetting to strip whitespace when fields have trailing padding.
  • Hardcoding slice positions instead of defining them in one place.
  • Assuming all lines have the same length, leading to short slices on truncated lines.

Variations

  1. Use `struct.unpack` with format codes for more complex fixed-width binary parsing.
  2. Read the file line by line with `open()` and call `parse_fixed_width` on each line.

Real-world use cases

  • Loading legacy mainframe reports where each column has a fixed character count.
  • Ingesting EDI transaction files that define data segments with fixed-width fields.
  • Processing log exports from older systems that pad fields with spaces for alignment.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Files & data

Related tutorials and quizzes for this topic.