Skip to main content

Python library parsing HL7 v2.x messages

Project description

Simple library for parsing messages of Health Level 7 (HL7) version 2.x.

HL7 is a communication protocol and message format for health care data. It is the de facto standard for transmitting data between clinical information systems and between clinical devices. The version 2.x series, which is often is a pipe delimited format is currently the most widely accepted version of HL7 (version 3.0 is an XML-based format).

python-hl7 currently only parses HL7 version 2.x messages into an easy to access data structure. The current implementation does not completely follow the HL7 specification, but is good enough to parse the most commonly seen HL7 messages. The library could potentially evolve into being fully complainant with the spec. The library could eventually also contain the ability to create HL7 v2.x messages.

python-hl7 parses HL7 into a series of wrapped hl7.Container objects. The there are specific subclasses of hl7.Container depending on the part of the HL7 message. The hl7.Container message itself is a subclass of a Python list, thus we can easily access the HL7 message as an n-dimensional list. Specically, the subclasses of hl7.Container, in order, are hl7.Message, hl7.Segment, and hl7.Field. Eventually additional containers will be added to fully support the HL7 specification.

As an example, let’s create a HL7 message:

>>> message = 'MSH|^~\&|GHH LAB|ELAB-3|GHH OE|BLDG4|200202150930||ORU^R01|CNTRL-3456|P|2.4
'
>>> message += 'PID|||555-44-4444||EVERYWOMAN^EVE^E^^^^L|JONES|196203520|F|||153 FERNWOOD DR.^^STATESVILLE^OH^35292||(206)3345232|(206)752-121||||AC555444444||67-A4335^OH^20030520
'
>>> message += 'OBR|1|845439^GHH OE|1045813^GHH LAB|1554-5^GLUCOSE|||200202150730||||||||555-55-5555^PRIMARY^PATRICIA P^^^^MD^^LEVEL SEVEN HEALTHCARE, INC.|||||||||F||||||444-44-4444^HIPPOCRATES^HOWARD H^^^^MD
'
>>> message += 'OBX|1|SN|1554-5^GLUCOSE^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F
'

We call the hl7.parse() command with string message:

>>> h = parse(message)

We get a hl7.Message object, wrapping a series of hl7.Segment objects.

>>> type(h)
<class 'hl7.Message'>

We can always get the HL7 message back.

>>> str(h) == message.strip()
True

Interestingly, this hl7.Message can be accessed as a list.

>>> isinstance(h, list)
True

There were 4 segments (MSH, PID, OBR, OBX):

>>> len(h)
4

We can extract the hl7.Segment from the hl7.Message instance.

>>> h[3]
[['OBX'], ['1'], ['SN'], ['1554-5', 'GLUCOSE', 'POST 12H CFST:MCNC:PT:SER/PLAS:QN'], [''], ['', '182'], ['mg/dl'], ['70_105'], ['H'], [''], [''], ['F']]

We can easily reconstitute this segment as HL7, using the appopriate separators.

>>> str(h[3])
'OBX|1|SN|1554-5^GLUCOSE^POST 12H CFST:MCNC:PT:SER/PLAS:QN||^182|mg/dl|70_105|H|||F'

We can extract individual elements of the message:

>>> h[3][3][1]
'GLUCOSE'
>>> h[3][5][1]
'182'

We can look up segments by the segment identifer:

>>> pid = segment('PID', h)
>>> pid[3][0]
'555-44-4444'

Project site: http://www.bitbucket.org/johnpaulett/python-hl7/wiki/Home

HL7 References:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hl7-0.1.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

hl7-0.1.0-py2.5.egg (10.8 kB view details)

Uploaded Source

File details

Details for the file hl7-0.1.0.tar.gz.

File metadata

  • Download URL: hl7-0.1.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for hl7-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b0c9bfa1cf89638f148e86a0004af0397fb44743a603843f96f7dce3a5ea5b61
MD5 3547482390ce0f3e97970587776da9e8
BLAKE2b-256 9006da1ba00b49bae490b3543e9d94de026a3bfa55761300d928a713fd5bf0e6

See more details on using hashes here.

File details

Details for the file hl7-0.1.0-py2.5.egg.

File metadata

  • Download URL: hl7-0.1.0-py2.5.egg
  • Upload date:
  • Size: 10.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for hl7-0.1.0-py2.5.egg
Algorithm Hash digest
SHA256 50105a2fd9f902823b73746d362737f6f520648b71223dfbb78a1b3dc72ea63a
MD5 165e77c6b1cd1bb4ccbbab97e6b39dd3
BLAKE2b-256 ba7f9b0d7d10b4b6b123147310a223095b6cd27985e7e40fa1ba4333ba6b0ea3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page