Reputation: 513
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.
Upvotes: 1
Views: 3440
Reputation: 43
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
Upvotes: 1
Reputation: 6436
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.
Upvotes: 1
Reputation: 1153
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.
Upvotes: 1