PyX12 - HIPAA X12 Parser and Validator

2.3.3 · active · verified Thu Apr 16

PyX12 is a Python library for validating, parsing, and converting HIPAA X12 EDI (Electronic Data Interchange) files. It supports various transaction sets and versions, primarily focusing on compliance and structural integrity. The current stable version on PyPI is 2.3.3. A bugfix release 2.3.4 is available on GitHub, and a Python 3-only version 3.0.0 is currently in release candidate phase, which will enforce Python 3.6+.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to parse an X12 EDI file using `X12Message` and iterate through its segments. It also shows the basic setup for `X12N` for validation, though full validation requires proper configuration of X12 map files (schemas) which are not included in the basic installation.

import os
from pyx12.x12message import X12Message
from pyx12.map_walker import X12N

# Create a dummy X12 file for demonstration
edi_data = """ISA*00*          *00*          *ZZ*SENDERID       *ZZ*RECEIVERID     *200101*1200*U*00401*000000001*0*T*:~GS*HI*SENDER*RECEIVER*20010101*1200*1*X*004010VICS~ST*837*0001*005010X222~BHT*0019*00*01*20010101*1200*CH~HL*1**20*1~PRV*BI*PXC*01~NM1*85*1*SMITH*JOHN*P***XX*1234567890~PER*IC*JOHN SMITH*TE*5555551212~SE*10*0001~GE*1*1~IEA*1*000000001~"""

# Save the EDI data to a temporary file
file_path = "test_837.edi"
with open(file_path, "w") as f:
    f.write(edi_data)

# Initialize X12Message with the file
try:
    # Using a dummy map_path for basic parsing, real validation requires actual maps
    # For a full validation, set X12_MAP_PATH_INI or pass map_path parameter
    # Example: map_path = os.environ.get('X12_MAP_PATH', './maps')
    msg = X12Message(file_path)

    # Iterate through segments
    print("\n--- Segments --- ")
    for segment in msg.walk_segments():
        print(f"Segment: {segment.get_tag()}, Elements: {segment.get_elements()}")

    # Perform basic validation (requires map files to be configured or provided)
    print("\n--- Validation (requires map files) ---")
    # Note: For this quickstart, actual validation will likely fail without proper map setup.
    # Configure map paths via environment variable or 'map_path' parameter in X12N.
    # os.environ['X12_MAP_PATH'] = 'path/to/your/map/files'
    walker = X12N(msg)
    # validation_result = walker.validate()
    # print(f"Validation Result: {validation_result.get_state()}")
    print("Note: Full validation requires X12 map files. See documentation for setup.")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Clean up the dummy file
    if os.path.exists(file_path):
        os.remove(file_path)

view raw JSON →