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.
Python code
24 linesfrom 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
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
- Use `struct.unpack` with format codes for more complex fixed-width binary parsing.
- 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
More from Files & data
- Append a Line to a Log File in Python easy
- Audit File Permissions Across a Project in Python easy
- Automatically Detect Corrupted Files Using SHA-256 Checksums in Python easy
- Automatically Highlight Data Validation Errors Inside Excel Files in Python easy
- Build a Command-Line To-Do List Application with Data Persistence in Python easy
- Build a File Index by Relative Path Hash Map in Python easy
Keep learning
Related tutorials and quizzes for this topic.