Install & Compatibility
Where this runs
tested against v1.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.9MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
FixedWidth
✓ import fixedwidth
✗ from fixedwidth import FixedWidth
This quickstart demonstrates how to define a fixed-width specification using a configuration dictionary, then use the `FixedWidth` class to parse a fixed-width string into a Python dictionary and serialize a dictionary back into a fixed-width string.
from fixedwidth import FixedWidth
# Define the fixed-width specification
# A field must have a start_pos and either an end_pos or a length.
# If both end_pos and length are provided, they must not conflict.
# A field may not have a default value if it is required.
# Supported types: 'string', 'integer', 'decimal'.
config = {
'name': {
'start_pos': 0,
'end_pos': 10,
'type': 'string',
'required': True,
'alignment': 'left',
'padding': ' ',
},
'age': {
'start_pos': 10,
'length': 3,
'type': 'integer',
'required': False,
'alignment': 'right',
'padding': '0',
},
'city': {
'start_pos': 13,
'length': 10,
'type': 'string',
'required': True,
'alignment': 'left',
'padding': ' ',
},
}
# Create a FixedWidth instance
fixed_width_spec = FixedWidth(config)
# Example 1: Read from a fixed-width line
fixed_width_line_to_read = "John Doe 025New York "
data_dict = fixed_width_spec.read(fixed_width_line_to_read)
print("Parsed data:", data_dict)
# Expected output: {'name': 'John Doe', 'age': 25, 'city': 'New York'}
# Example 2: Write to a fixed-width line
data_to_write = {'name': 'Jane Smith', 'age': 30, 'city': 'Los Angeles'}
fixed_width_line_written = fixed_width_spec.write(data_to_write)
print("Formatted line:", fixed_width_line_written)
# Expected output: "Jane Smith030Los Angeles "
Debug
Known issues
gotchaThe library requires a complete schema definition for all columns up to the maximum width specified. Attempting to parse or format data with gaps in the column definitions or an incomplete schema can lead to unexpected errors or incorrect parsing, as it expects a contiguous layout.fixEnsure your configuration dictionary accounts for all character positions in the fixed-width string, even if some fields are merely padding or ignored data.
affects: 1.x
gotchaConfiguration dictionary fields are strict. A field must define either `end_pos` or `length` in conjunction with `start_pos`. If both `end_pos` and `length` are provided, they must not conflict. Additionally, a `required` field cannot be assigned a `default` value.fixCarefully review the field definitions in your configuration dictionary to adhere to the specified constraints regarding position, length, required status, and default values.
affects: 1.x
deprecatedThe `fixedwidth` library (v1.3) has not seen an update since June 2018. While stable and reported to be used in production, users should be aware of its unmaintained status, which might affect compatibility with newer Python versions or address new edge cases. For actively maintained alternatives, consider libraries like `pyfixwidth` or `pandas.read_fwf` for more robust fixed-width file parsing, especially for large datasets.fixAssess the long-term maintenance risk. For new projects or if encountering compatibility issues, consider migrating to a more actively maintained library like `pyfixwidth` or `pandas.read_fwf`.
affects: 1.x (since 2018)
Upgrade
Version history
1.3latest on PyPI · released Jun 7, 2018
Audit
Dependencies
No dependency data recorded yet.