Pydifact is a library that aims to provide complete support for reading and writing EDIFACT files. These file format, despite being old, is still a standard in many business cases. In Austria e.g., it is used for the transfer of medical reports between medical doctors.
Maintained. Last release 8 months ago, commits in 20 of the last 26 weeks. Weaker on contributor spread and security practices.
pip install pydifactpydifact
A Python library to parse and serialize UN/EDIFACT interchanges.
Preamble
This is a port of metroplex-systems/edifact to Python. Thanks here at the start to Craig Duncan for this cool piece of software. Porting was like a breeze due to the high code quality there. All credits for the initial code here go to him, I just did the translation to Python(3), some "pythonifications" of the code and little improvements.
Why another EDIFACT library?
Because I did not find a decent UN/EDIFACT library for Python, so I decided to port one of the available good PHP libraries to Python. Here is the result.
ATM this is a Work In Progress, the API is not stable yet. Feel free to help.
Install
uv pip install pydifact
# or
python -m pip install pydifact
However, it is not marked as stable yet, so the pypi version, including documentation and code examples, could differ from the latest git code. If in doubt, use the git version. You should use uv for the development process:
git clone https://github.com/nerdocs/pydifact.git
cd pydifact
uv sync
Usage
To read a full Interchange from a file or string, take the Interchange class and
iter over the messages and segments:
from pydifact.segmentcollection import Interchange
interchange = Interchange.from_file("./tests/data/wikipedia.edi")
interchange = Interchange.from_str(
"UNA:+,? '"
"UNB+UNOC:1+1234+3333+200102:2212+42'"
"UNH+42z42+PAORES:93:1:IA'"
"MSG+1:45'"
"IFT+3+XYZCOMPANY AVAILABILITY'"
"ERC+A7V:1:AMD'"
"UNT+5+42z42'UNZ+2+42'"
)
for message in interchange.get_messages():
for segment in message.segments:
print(f"Segment tag: {segment.tag}, content: {segment.elements}")
You may also want to iterate directly on segments :
from pydifact.segmentcollection import Interchange
interchange = Interchange.from_str(
"UNA:+,? '"
"UNB+UNOC:1+1234+3333+200102:2212+42'"
"UNH+42z42+PAORES:93:1:IA'"
"MSG+1:45'"
"IFT+3+XYZCOMPANY AVAILABILITY'"
"ERC+A7V:1:AMD'"
"UNT+5+42z42'UNZ+2+42'"
)
for segment in interchange.segments:
print(f"Segment tag: {segment.tags}, content: {segment.elements}")
Or you can create an EDI interchange on the fly:
from pydifact.segmentcollection import Interchange
from pydifact.segments import Segment
interchange = Interchange(syntax_identifier=("IBMA",1),
sender="MeMyselfAndIrene",
recipient="TheOtherOne",
control_reference="KLuzs7c6")
interchange.add_segment(Segment("QTY", ["12", "3"]))
print(interchange.serialize())