python-hl7 is a simple library for parsing messages of Health Level 7 (HL7) version 2.x into Python objects.
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. Specifically, 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\r'
>>> 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\r'
>>> 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\r'
>>> 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:
>>> import hl7
>>> h = hl7.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:
>>> unicode(h) == message
True
Interestingly, 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]
[[u'OBX'], [u'1'], [u'SN'], [u'1554-5', u'GLUCOSE', u'POST 12H CFST:MCNC:PT:SER/PLAS:QN'], [u''], [u'', u'182'], [u'mg/dl'], [u'70_105'], [u'H'], [u''], [u''], [u'F']]
We can easily reconstitute this segment as HL7, using the appropriate separators:
>>> unicode(h[3])
u'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]
u'GLUCOSE'
>>> h[3][5][1]
u'182'
We can look up segments by the segment identifier, either via hl7.Message.segments() or via the traditional dictionary syntax:
>>> h.segments('OBX')[0][3][1]
u'GLUCOSE'
>>> h['OBX'][0][3][1]
u'GLUCOSE'
Since many many types of segments only have a single instance in a message (e.g. PID or MSH), hl7.Message.segment() provides a convienance wrapper around hl7.Message.segments() that returns the first matching hl7.Segment:
>>> h.segment('PID')[3][0]
u'555-44-4444'
Returns a instance of the hl7.Message that allows indexed access to the data elements.
Note
HL7 usually contains only ASCII, but can use other character sets (HL7 Standards Document, Section 1.7.1). Therefore, python-hl7 works on Python unicode strings. hl7.parse() will accept ASCII-only strings and automatically convert them into unicode. However, if the message contains non-ASCII characters, it is the responsibility of the caller of hl7.parse() to properly convert the message string to unicode first.
>>> h = hl7.parse(message)
Return type: | hl7.Message |
---|
Determines whether a line looks like an HL7 message. This method only does a cursory check and does not fully validate the message.
Return type: | bool |
---|
Abstract root class for the parts of the HL7 message.
Join a the child containers into a single string, separated by the self.separator. This method acts recursively, calling the children’s __unicode__ method. Thus unicode() is the approriate method for turning the python-hl7 representation of HL7 into a standard string.
>>> unicode(h) == message
True
Representation of an HL7 message. It contains a list of hl7.Segment instances.
Returns the requested segments from the parsed message that are identified by the segment_id (e.g. OBR, MSH, ORC, OBX).
>>> h.segments('OBX')
[[[u'OBX'], [u'1'], ...]]
Return type: | list of hl7.Segment |
---|
Gets the first segment with the segment_id from the parsed message.
>>> h.segment('PID')
[[u'PID'], ...]
Return type: | hl7.Segment |
---|
Index or segment-based lookup.
If key is an integer, __getitem__ acts list a list, returning the hl7.Segment held at that index:
>>> h[1]
[[u'PID'], ...]
If the key is a string, __getitem__ acts like a dictionary, returning all segments whose segment_id is key (alias of hl7.Message.segments()).
>>> h['OBX']
[[[u'OBX'], [u'1'], ...]]
Return type: | hl7.Segment or list of hl7.Segment |
---|
Second level of an HL7 message, which represents an HL7 Segment. Traditionally this is a line of a message that ends with a carriage return and is separated by pipes. It contains a list of hl7.Field instances.
Third level of an HL7 message, that traditionally is surrounded by pipes and separated by carets. It contains a list of strings.
The source code is available at http://github.com/johnpaulett/python-hl7
The test suite is located in tests/ and can be run via setup.py:
$ python setup.py test
...
----------------------------------------------------------------------
Ran 17 tests in 0.005s
OK
Make sure the documentation is still valid:
$ pushd docs && make html && make doctest && popd
...
Doctest summary
===============
23 tests
0 failures in tests
0 failures in setup code
...
Copyright (C) 2009-2011 John Paulett (john -at- paulett.org)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
3. The name of the author may not be used to endorse or promote
products derived from this software without specific prior
written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
HL7 References: