python-hl7 – HL7 v2.x Message Parser

0.4.5 · active · verified Tue Mar 17

A simple Python library for parsing HL7 v2.x messages. Provides segment, field, and component-level access to HL7 messages, along with MLLP (Minimal Lower Layer Protocol) client support. Does not handle HL7 FHIR (v4); it is strictly for the pipe-delimited v2.x wire format.

Warnings

Install

Imports

Quickstart

Parse an HL7 v2.x message and access segments and fields.

import hl7

raw = 'MSH|^~\\&|SND|SND_FAC|RCV|RCV_FAC|20230101120000||ADT^A01|MSG00001|P|2.3\rPID|||12345^^^MRG||DOE^JOHN||19800101|M\r'
message = hl7.parse(raw)

# Access fields using segment(field) notation
print(message.segment('MSH'))       # Full MSH segment
print(message['MSH.9'])              # Message type: ADT^A01
print(message['PID.5'])              # Patient name: DOE^JOHN
print(message.segment('PID')[5])     # Same: DOE^JOHN

view raw JSON →