{"id":4004,"library":"fixedwidth","title":"Two-way fixed-width <--> Python dict converter","description":"The `fixedwidth` library provides a straightforward two-way converter for interacting with fixed-width data files or strings in Python. It facilitates easy serialization of Python dictionaries into fixed-width format and deserialization back into dictionaries, based on a user-defined configuration specification. Last updated in 2018 (v1.3), it has been used in production environments for several years and is considered stable, though not actively developed.","status":"maintenance","version":"1.3","language":"en","source_language":"en","source_url":"https://github.com/ShawnMilo/fixedwidth","tags":["fixed-width","parser","serialization","data-conversion","legacy-data"],"install":[{"cmd":"pip install fixedwidth","lang":"bash","label":"Install stable version"}],"dependencies":[],"imports":[{"symbol":"FixedWidth","correct":"from fixedwidth import FixedWidth"}],"quickstart":{"code":"from fixedwidth import FixedWidth\n\n# Define the fixed-width specification\n# A field must have a start_pos and either an end_pos or a length.\n# If both end_pos and length are provided, they must not conflict.\n# A field may not have a default value if it is required.\n# Supported types: 'string', 'integer', 'decimal'.\nconfig = {\n    'name': {\n        'start_pos': 0,\n        'end_pos': 10,\n        'type': 'string',\n        'required': True,\n        'alignment': 'left',\n        'padding': ' ',\n    },\n    'age': {\n        'start_pos': 10,\n        'length': 3,\n        'type': 'integer',\n        'required': False,\n        'alignment': 'right',\n        'padding': '0',\n    },\n    'city': {\n        'start_pos': 13,\n        'length': 10,\n        'type': 'string',\n        'required': True,\n        'alignment': 'left',\n        'padding': ' ',\n    },\n}\n\n# Create a FixedWidth instance\nfixed_width_spec = FixedWidth(config)\n\n# Example 1: Read from a fixed-width line\nfixed_width_line_to_read = \"John Doe  025New York   \"\ndata_dict = fixed_width_spec.read(fixed_width_line_to_read)\nprint(\"Parsed data:\", data_dict)\n# Expected output: {'name': 'John Doe', 'age': 25, 'city': 'New York'}\n\n# Example 2: Write to a fixed-width line\ndata_to_write = {'name': 'Jane Smith', 'age': 30, 'city': 'Los Angeles'}\nfixed_width_line_written = fixed_width_spec.write(data_to_write)\nprint(\"Formatted line:\", fixed_width_line_written)\n# Expected output: \"Jane Smith030Los Angeles \"","lang":"python","description":"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."},"warnings":[{"fix":"Ensure your configuration dictionary accounts for all character positions in the fixed-width string, even if some fields are merely padding or ignored data.","message":"The 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.","severity":"gotcha","affected_versions":"1.x"},{"fix":"Carefully review the field definitions in your configuration dictionary to adhere to the specified constraints regarding position, length, required status, and default values.","message":"Configuration 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.","severity":"gotcha","affected_versions":"1.x"},{"fix":"Assess 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`.","message":"The `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.","severity":"deprecated","affected_versions":"1.x (since 2018)"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}