Search code examples
pythonx12

places to look for code to convert a data file to X12 format?


I have a data file that looks similar to:

data1~|~data2~|~data3~!~data1~|~data2~|~data3~!~data1~|~data2~|~data3~!~data1~|~data2~|~data3~!~

The fields are delimited by ~|~ (tilde/pipe/tilde). The row/records are delimited by ~!~.

The goal will be to massage this into an X12 formatted file. I will have many files and many records.. thousands of each.. I just started the project so I am exploring solutions.

I have done a little bit of coding in python so maybe that would work but not sure.

I am looking for suggestions or a library that may contain something to look at at to get started.


Solution

  • data = "data1~|~data2~|~data3~!~data1~|~data2~|~data3~!~data1~|~data2~|~data3~!~data1~|~data2~|~data3~!~"
    
    ## parse rows
    rows = data.split("~!~")
    
    ## Parse Columns 
    final = [x.split("~|~") for x in rows]
    print(final)
    

    result is:

    [['data1', 'data2', 'data3'], ['data1', 'data2', 'data3'], ['data1', 'data2', 'data3'], ['data1', 'data2', 'data3'], ['']]
    

    The next step would be converting to XML.

    Then once you have it converted to XML, use: https://pypi.python.org/pypi/pyx12/2.1.1 to convert it to X12

    Then like magic, you should be done!