简体   繁体   English

HL7 使用 python 解析多条消息

[英]HL7 Parsing with Multiple Messages using python

I am working on HL7 file parsing.我正在处理 HL7 文件解析。

On daily basis I will get the HL7 file logs, which contain multiple messages inside a single file.每天我都会获取 HL7 文件日志,其中包含单个文件中的多条消息。

I have chosen the http://python-hl7.readthedocs.org/en/latest/api.html library for parsing, but this is parsing only one message.我选择了http://python-hl7.readthedocs.org/en/latest/api.html库进行解析,但这只解析一条消息。 If i give a file stream, it will not parse it.如果我给一个文件 stream,它不会解析它。

How can we parse multiple messages in a file stream using python.我们如何使用 python 解析文件 stream 中的多条消息。

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. 如果文件包含HL7消息的开始和结束字符,则可以将其用作标记。 Another way would be to split the file at each MSH segment, because each message should contain a MSH as the first segment. 另一种方法是在每个MSH段中分割文件,因为每个消息都应包含一个MSH作为第一段。

Multiple messages in one file should be enclosed with the HL7 Batch File Protocol . HL7批处理文件协议应在一个文件中包含多条消息。 You can test this with hl7.isfile(line) and you get the messages with hl7.split_file(hl7file) . 您可以使用hl7.isfile(line)对此进行测试,并使用hl7.split_file(hl7file)获得消息。

from the python hl7 api parse documentation 来自python hl7 api解析文档

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:这是我的解决方案,我在 MSH 段拆分,以便我可以分别解析消息:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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