{"id":8477,"library":"pydifact","title":"Pydifact: EDIFACT Parser and Serializer","description":"Pydifact is a Python library designed to provide comprehensive support for reading and writing EDIFACT (Electronic Data Interchange For Administration, Commerce and Transport) files. Despite being an older format, EDIFACT remains a standard in many business sectors, such as for the transfer of medical reports in Austria. This library is a work in progress, with its current version being 0.2.3, and the API is not yet stable, implying a potentially rapid release cadence with breaking changes.","status":"active","version":"0.2.3","language":"en","source_language":"en","source_url":"https://github.com/nerdocs/pydifact","tags":["EDIFACT","parser","serializer","business","data-exchange","EDI"],"install":[{"cmd":"pip install pydifact","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Requires Python 3.10 or newer for full compatibility and to avoid `AttributeError` with `collections.Iterable`.","package":"python","optional":false}],"imports":[{"note":"`SegmentCollection` was removed/deprecated in versions 0.2.x+ in favor of `Interchange`.","wrong":"from pydifact.segmentcollection import SegmentCollection","symbol":"Interchange","correct":"from pydifact.segmentcollection import Interchange"},{"symbol":"Segment","correct":"from pydifact.segments import Segment"}],"quickstart":{"code":"from pydifact.segmentcollection import Interchange\nfrom pydifact.segments import Segment\n\n# Example EDIFACT data (Interchange containing one message)\nedifact_data = (\n    \"UNA:+,? '\\n\"\n    \"UNB+UNOC:1+1234+3333+200102:2212+42'\\n\"\n    \"UNH+42z42+PAORES:93:1:IA'\\n\"\n    \"MSG+1:45'\\n\"\n    \"IFT+3+XYZCOMPANY AVAILABILITY'\\n\"\n    \"ERC+A7V:1:AMD'\\n\"\n    \"UNT+5+42z42'\\n\"\n    \"UNZ+2+42'\"\n)\n\n# --- Reading an EDIFACT interchange from a string ---\ninterchange = Interchange.from_str(edifact_data)\nprint(\"\\n--- Reading Interchange ---\")\nfor message in interchange.get_messages():\n    for segment in message.segments:\n        print(f\"Segment tag: {segment.tag}, content: {segment.elements}\")\n\n# --- Creating an EDIFACT interchange ---\nnew_interchange = Interchange()\nnew_interchange.add_segment(Segment(\"UNA\", [\":\", \"+\", \",\", \"?\", \" \", \"'\"])) # Optional, if custom control characters are needed\nnew_interchange.add_segment(Segment(\"UNB\", [\"UNOC:1\", \"SENDER\", \"RECEIVER\", \"20230101:1000\", \"REF123\"]))\nnew_message = new_interchange.new_message(\"ORDER\", \"D\", \"96A\", \"UN\")\nnew_message.add_segment(Segment(\"BGM\", [\"220\", \"ORDER123\"]))\nnew_message.add_segment(Segment(\"DTM\", [\"137:20230101:1000\"]))\nnew_interchange.add_message(new_message)\n\nprint(\"\\n--- Serializing Interchange ---\")\nprint(new_interchange.serialize(break_lines=True))\n","lang":"python","description":"This quickstart demonstrates how to parse an EDIFACT interchange from a string and iterate through its messages and segments. It also shows how to construct a new interchange programmatically and serialize it into an EDIFACT string."},"warnings":[{"fix":"Pin your `pydifact` version in `requirements.txt` to prevent unexpected upgrades, or thoroughly review the changelog before updating.","message":"The API is not yet stable and frequent breaking changes can occur between minor versions. Always consult the `CHANGELOG.md` before upgrading.","severity":"breaking","affected_versions":"All versions < 1.0.0"},{"fix":"Replace `SegmentCollection` instances with `Interchange` for top-level EDIFACT documents and `Message` for individual messages within an interchange.","message":"The `SegmentCollection` class has been removed/deprecated. Functionality has been moved to the `Interchange` and `Message` classes.","severity":"breaking","affected_versions":"0.2.x+"},{"fix":"Update `Segment()` instantiations to `Segment('TAG', ['element1', 'element2'])` instead of `Segment(['element1', 'element2'])` (if that syntax was previously supported or inferred).","message":"Calls to `Segment()` now *must* provide the segment tag name as the first positional parameter.","severity":"breaking","affected_versions":"0.2.x+"},{"fix":"Ensure your project runs on Python 3.10 or a newer version to use `pydifact` 0.2.x and above. For older Python versions, you must use an older `pydifact` release (e.g., < 0.2.0).","message":"Support for Python versions older than 3.10 has been dropped.","severity":"breaking","affected_versions":"0.2.x+"},{"fix":"Always use keyword arguments (e.g., `Characters(data_separator='|')`) when instantiating `pydifact.control.Characters` to ensure forward compatibility.","message":"When defining custom control characters, if you previously used positional arguments instead of keyword arguments, the introduction of a 'reserved' character parameter in some versions might shift argument positions and cause unexpected behavior.","severity":"gotcha","affected_versions":"0.1.9"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Upgrade `pydifact` to version 0.2.0 or higher: `pip install --upgrade pydifact`. These versions explicitly require Python >=3.10 and correctly use `collections.abc.Iterable`.","cause":"This error typically occurs when using an older version of `pydifact` (e.g., <0.2.0) with Python 3.10 or newer. Python 3.10 removed `collections.Iterable` in favor of `collections.abc.Iterable`.","error":"AttributeError: module 'collections' has no attribute 'Iterable'"},{"fix":"Implement proper error handling (e.g., `try-except EdiFactSyntaxError`) around parsing operations, or preprocess/validate EDIFACT files to ensure they conform to the expected syntax before feeding them to pydifact.","cause":"Pydifact is designed to raise `EdiFactSyntaxError` when it encounters malformed EDIFACT syntax, rather than silently attempting to parse or ignore errors.","error":"EdiFactSyntaxError: ..."},{"fix":"After creating an `Interchange` object, you must explicitly call `interchange.get_messages()` to retrieve an iterable of `Message` objects. Then, iterate over each `Message` to access its `segments` property.","cause":"Users sometimes confuse the `Interchange` and `Message` classes or assume direct iteration over `Interchange` yields messages. The `Interchange` object represents the entire EDIFACT file, which can contain multiple messages.","error":"My code only reads the first message, not all messages in an interchange."}]}