python - 如何使用 python HL7Apy 正确解析 HL7 消息?
问题描述
这是我的代码:
from hl7apy.parser import parse_message
hl7 = open("hl7.txt", "r").read()
msg = parse_message(hl7)
print(msg.children)
结果:
[<Segment MSH>]
它只显示第一段,看起来很简单,但我不知道我做错了什么。
我尝试过从文本文件中直接传递消息,甚至使用另一条 HL7 消息传递,但总是得到相同的结果。
这是消息:
MSH|^~\&|SendingAPP|TEST|||20080617143943||ORU^R01|1|P|2.3.1||||||UNICODE
PID|1||7393670^^^^MR||Joan^JIang||19900804000000|Female
PV1|1||nk^^001
OBR|1||20071207011|00001^Automated Count^99MRC||20080508140600|20080508150616|||John||||20080508150000||||||||||HM||||||||TEST
解决方案
我认为您的问题是 HL7apy 的 MLLP 常量是Carriage return \r
。如果您替换新行字符\n
,组将解析得很好
from hl7apy.parser import parse_message
from hl7apy.core import Group, Segment
hl7 = """
MSH|^~\&|SendingAPP|TEST|||20080617143943||ORU^R01|1|P|2.3.1||||||UNICODE
PID|1||7393670^^^^MR||Joan^JIang||19900804000000|Female PV1|1||nk^^001
OBR|1||20071207011|00001^Automated
Count^99MRC||20080508140600|20080508150616|||John||||20080508150000||||||||||HM||||||||TEST
"""
msg = parse_message(hl7.replace('\n', '\r'), find_groups=True, validation_level=2)
print(msg.children)
print(msg.children[1].children)
for segment in msg.children:
if isinstance(segment, Segment):
for attribute in segment.children:
print(attribute, attribute.value)
if isinstance(segment, Group):
for group in segment.children:
for group_segment in group.children:
for attribute in group_segment.children:
print(attribute, attribute.value)
推荐阅读
- selenium - 有什么方法可以使用 xpath 单击 Facebook Messenger 输入字段?
- postgresql - 将慢速 PostgreSQL 查询记录到 Docker 控制台
- django-forms - 通过联系表单 Django 发送带有附件的电子邮件
- javascript - 每秒更新 useEffect 中的状态 10 秒
- javascript - 为什么 React App 在网页上出现两次?
- ruby-on-rails - 有没有办法在 Ruby 中读取不同的文件类型?
- python - 为什么它不跳到其他(硒)有什么原因吗?
- android - 如何使用 Firestore 分页适配器在客户端过滤数据?
- html - 如何在 Android 上为 Linux/Ubuntu 制作滑动菜单?
- javascript - html按钮不会变得不可见