简体   繁体   中英

HL7 Parsing with Multiple Messages using python

I am working on HL7 file parsing.

On daily basis I will get the HL7 file logs, which contain multiple messages inside a single file.

I have chosen the http://python-hl7.readthedocs.org/en/latest/api.html library for parsing, but this is parsing only one message. If i give a file stream, it will not parse it.

How can we parse multiple messages in a file stream using python.

You might want to split the file into individual messages before parsing. If the file contains HL7 message start and end characters, then You can use those as markers. Another way would be to split the file at each MSH segment, because each message should contain a MSH as the first segment.

Multiple messages in one file should be enclosed with the HL7 Batch File Protocol . You can test this with hl7.isfile(line) and you get the messages with hl7.split_file(hl7file) .

from the python hl7 api parse documentation

hl7.isfile(line)
   Files are wrapped in FHS / FTS FHS = file header segment FTS = file trailer segment

hl7.split_file(hl7file)
   Given a file, split out the messages. Does not do any validation on the message. Throws away batch and file segments.

Here is my solution, I split at the MSH segment so that I can parse the msgs separately:

with open('file_path/file_name.txt') as msgs:
start = False
for line in msgs.readlines():
    if line[:3] == 'MSH':
        if start:
            parsed_msg = hl7.parse(msg)
            print(parsed_msg)
            start = False
        msg = line
        start = True
    else:
        msg += line

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM