Last active
July 9, 2024 20:07
-
-
Save prschmid/801e86891026a39b1fb4df7178828321 to your computer and use it in GitHub Desktop.
Convert an HL7 message to a dictionary.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"""Converting HL7 messages to dictionaries | |
Example Usage: | |
import pprint | |
from hl7apy.parser import parse_message | |
# Taken from http://hl7apy.org/tutorial/index.html#elements-manipulation | |
s = """MSH|^~\&|GHH_ADT||||20080115153000||ADT^A01^ADT_A01|0123456789|P|2.5||||AL | |
EVN||20080115153000||AAA|AAA|20080114003000 | |
PID|1||566-554-3423^^^GHH^MR||EVERYMAN^ADAM^A|||M|||2222 HOME STREET^^ANN ARBOR^MI^^USA||555-555-2004~444-333-222|||M | |
NK1|1|NUCLEAR^NELDA^W|SPO|2222 HOME STREET^^ANN ARBOR^MI^^USA""" | |
pprint.pprint(hl7_str_to_dict(s)) | |
Yup, it's as simple as that. | |
""" | |
from hl7apy.parser import parse_message | |
def hl7_str_to_dict(s, use_long_name=True): | |
"""Convert an HL7 string to a dictionary | |
:param s: The input HL7 string | |
:param use_long_name: Whether or not to user the long names | |
(e.g. "patient_name" instead of "pid_5") | |
:returns: A dictionary representation of the HL7 message | |
""" | |
s = s.replace("\n", "\r") | |
m = parse_message(s) | |
return hl7_message_to_dict(m, use_long_name=use_long_name) | |
def hl7_message_to_dict(m, use_long_name=True): | |
"""Convert an HL7 message to a dictionary | |
:param m: The HL7 message as returned by :func:`hl7apy.parser.parse_message` | |
:param use_long_name: Whether or not to user the long names | |
(e.g. "patient_name" instead of "pid_5") | |
:returns: A dictionary representation of the HL7 message | |
""" | |
if m.children: | |
d = {} | |
for c in m.children: | |
name = str(c.name).lower() | |
if use_long_name: | |
name = str(c.long_name).lower() if c.long_name else name | |
dictified = hl7_message_to_dict(c, use_long_name=use_long_name) | |
if name in d: | |
if not isinstance(d[name], list): | |
d[name] = [d[name]] | |
d[name].append(dictified) | |
else: | |
d[name] = dictified | |
return d | |
else: | |
return m.to_er7() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment