Filter Segments Only From HL7 Messages Using HL7APY

881 Views Asked by At

I want to grab the OBX segments, and then field # 3, the observation identifier. My code currently iterates everything, trying to find a solution to output just OBX.field3, for a dataset with multiple hl7 messages.

hl7 = open(r"file.txt", "r").read()



try:
msg = parser.parse_message(hl7.replace('\n', '\r'), find_groups=True, validation_level=2)
except UnsupportedVersion:
msg = parser.parse_message(hl7.replace('\n', '\r'), find_groups=True, validation_level=2)


indent = "    "
indent_seg = "    "
indent_fld = "        "



def subgroup (group, indent):
indent = indent + "    "
print (indent , group)
for group_segment in group.children:
if isinstance(group_segment, Group):
subgroup (group_segment)
else:
print(indent_seg, indent ,group_segment)
for attribute in group_segment.children:
print(indent_fld, indent ,attribute, attribute.value)

def showmsg (msg):
print(msg.children[1])
for segment in msg.children:
if isinstance(segment, Segment):
print (indent ,segment)
for attribute in segment.children:
print(indent_fld, indent, attribute, attribute.value)
if isinstance(segment, Group):
for group in segment.children:
print (indent,group)
for group_segment in group.children:
if isinstance (group_segment, Group): 
subgroup (group_segment, indent)
else:
print(indent_seg, indent ,group_segment)
for attribute in group_segment.children:
print(indent_fld, indent, attribute, attribute.value)

showmsg (msg)`enter code here
1

There are 1 best solutions below

0
sqlab On

If you change find_groups=True to find_groups=False in parser.parse_messsage(.. you need only this showmsg

def showmsg (msg):
    for segment in msg.children:
        if segment.name == 'OBX':
            print (indent ,segment)
            for attribute in segment.children:
                if attribute.name == 'OBX_3':
                    print(indent_fld, indent, attribute, attribute.value)