使用 hl7apy 解析 HL7 时出错
Error in parsing HL7 using hl7apy
我正在使用 hl7apy 解析 python 中的 hl7 文件,我正在关注 this link。当我使用 sample.hl7 时,我得到了想要的结果,但是当我使用我自己的 hl7 文件时,我得到了以下错误
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "hl7apy/parser.py", line 82, in parse_message
m.structure_by_name)
File "hl7apy/parser.py", line 144, in parse_segments
reference))
File "hl7apy/parser.py", line 189, in parse_segment
reference=reference)
File "hl7apy/core.py", line 1564, in __init__
validation_level, traversal_parent)
File "hl7apy/core.py", line 632, in __init__
self._find_structure(reference)
File "hl7apy/core.py", line 808, in _find_structure
structure = ElementFinder.get_structure(self, reference)
File "hl7apy/core.py", line 524, in get_structure
raise InvalidName(element.classname, element.name)
hl7apy.exceptions.InvalidName: Invalid name for Segment:
我不明白我做错了什么。
编辑:
这是我正在使用的示例。
MSH|^~\&|SQ|BIN|SMS|BIN|20121009151949||ORU^R01|120330003918|P|2.2
PID|1|K940462|T19022||TEIENT|JYSHEE|1957009|F|MR^^RM^MR^DR^MD^3216|7|0371 HOES LANE^0371 HOES LANE^NORTH CENTRE^FL^0854^INDIA^P^98|^UA^|(21)2-921|203960|ENG^ENGLISH^HL7096^ENG^ENGLISH^9CLAN|U|^HINU^|^^^T1M|05-1-900||||NW HAVEN||||PAS|NOTH CETRE|
PV1|1|I|BDE^BDE||||960^FALK,HENRY^^^MD|||MED|||||||960^FALK,HENRY^^^MD||22599|||||||||||||||||||||||||20160613102300||||
ORC|RE|10112|1705||D||^^^20103102300^216061102300||201208100924|PS||10084^BRUCE^PALTHROW|||201606310230|
OBR|1|10112|1705|1786-6^HEMOGOI A1C|||201606131300|201606131300||SGR||||201208056||1029^BONE,EAN|3-266-91|||||201280058||CH|F||R^^^2012070957|||||104^VRNEY,SCT|
OBX|1|NM|1856-6^LOINC^LN^HEMOGOI A1C^L||5.9|%|4.2-6.3||||F|||20160613|A^^L
代码: 这是我用来解析我的 hl7 文件的代码和我在解析上面提到的示例 hl7 文件时使用的相同代码 link。
from hl7apy import parser
from hl7apy.exceptions import UnsupportedVersion
hl7 = open('ICD9.hl7', 'r').read()
try:
m = parser.parse_message(hl7)
except UnsupportedVersion:
m = parser.parse_message(hl7.replace("n", "r"))
您应该在此处显示您文件中的消息以获得明确的答案。
但最可能的原因是,您的文件内容不符合 HL7 规则。您确定使用了正确的段分隔符(ASCII 13 或 HEX 0D)吗?您是否使用非标准细分名称?
只需检查 Free Online HL7 Messages Validation 即可得到这些
ID ELEMENT_TYPE POSITION LINE_NO VALIDATION ERROR
1 Field MSH.9.3 1 Component required
2 Field PID.7 2 Invalid date time format : '1957009'
3 Component PID.9.7 2 Invalid table entry value : '3216' for table Name Type
4 Component PID.9.7 2 Value '3216' length (4) exceed limit (1)
5 Component PID.11.6 2 Invalid table entry value : 'INDIA' for table Country Code
6 Component PID.11.6 2 Value 'INDIA' length (5) exceed limit (3)
7 Field PID.12 2 Field should not contain component(s)
8 Component PID.18.1 2 Field required but has no value.
9 Field ORC.5 4 Invalid table entry value : 'D' for table Order status
10 Component ORC.7.4 4 Invalid date time format : '20103102300'
11 Component ORC.7.5 4 Invalid date time format : '216061102300'
12 Field ORC.15 4 Invalid date time format : '201606310230'
13 Field OBR.14 5 Invalid date time format : '201208056'
14 Field OBR.22 5 Invalid date time format : '201280058'
15 SubComponent OBR.27.1.1 5 Invalid numeric format : 'R'
16 Component OBR.27.4 5 Invalid date time format : '2012070957'
17 Component OBR.32.2 5 Invalid date time format : 'VRNEY,SCT'
但这并不能解释你的错误信息。您确定您阅读了消息文件并解析了内容吗?
您的代码有误。应该是
hl7.replace("\n", "\r")
如果要替换错误的段分隔符。
我正在使用 hl7apy 解析 python 中的 hl7 文件,我正在关注 this link。当我使用 sample.hl7 时,我得到了想要的结果,但是当我使用我自己的 hl7 文件时,我得到了以下错误
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "hl7apy/parser.py", line 82, in parse_message
m.structure_by_name)
File "hl7apy/parser.py", line 144, in parse_segments
reference))
File "hl7apy/parser.py", line 189, in parse_segment
reference=reference)
File "hl7apy/core.py", line 1564, in __init__
validation_level, traversal_parent)
File "hl7apy/core.py", line 632, in __init__
self._find_structure(reference)
File "hl7apy/core.py", line 808, in _find_structure
structure = ElementFinder.get_structure(self, reference)
File "hl7apy/core.py", line 524, in get_structure
raise InvalidName(element.classname, element.name)
hl7apy.exceptions.InvalidName: Invalid name for Segment:
我不明白我做错了什么。
编辑: 这是我正在使用的示例。
MSH|^~\&|SQ|BIN|SMS|BIN|20121009151949||ORU^R01|120330003918|P|2.2
PID|1|K940462|T19022||TEIENT|JYSHEE|1957009|F|MR^^RM^MR^DR^MD^3216|7|0371 HOES LANE^0371 HOES LANE^NORTH CENTRE^FL^0854^INDIA^P^98|^UA^|(21)2-921|203960|ENG^ENGLISH^HL7096^ENG^ENGLISH^9CLAN|U|^HINU^|^^^T1M|05-1-900||||NW HAVEN||||PAS|NOTH CETRE|
PV1|1|I|BDE^BDE||||960^FALK,HENRY^^^MD|||MED|||||||960^FALK,HENRY^^^MD||22599|||||||||||||||||||||||||20160613102300||||
ORC|RE|10112|1705||D||^^^20103102300^216061102300||201208100924|PS||10084^BRUCE^PALTHROW|||201606310230|
OBR|1|10112|1705|1786-6^HEMOGOI A1C|||201606131300|201606131300||SGR||||201208056||1029^BONE,EAN|3-266-91|||||201280058||CH|F||R^^^2012070957|||||104^VRNEY,SCT|
OBX|1|NM|1856-6^LOINC^LN^HEMOGOI A1C^L||5.9|%|4.2-6.3||||F|||20160613|A^^L
代码: 这是我用来解析我的 hl7 文件的代码和我在解析上面提到的示例 hl7 文件时使用的相同代码 link。
from hl7apy import parser
from hl7apy.exceptions import UnsupportedVersion
hl7 = open('ICD9.hl7', 'r').read()
try:
m = parser.parse_message(hl7)
except UnsupportedVersion:
m = parser.parse_message(hl7.replace("n", "r"))
您应该在此处显示您文件中的消息以获得明确的答案。
但最可能的原因是,您的文件内容不符合 HL7 规则。您确定使用了正确的段分隔符(ASCII 13 或 HEX 0D)吗?您是否使用非标准细分名称?
只需检查 Free Online HL7 Messages Validation 即可得到这些
ID ELEMENT_TYPE POSITION LINE_NO VALIDATION ERROR
1 Field MSH.9.3 1 Component required
2 Field PID.7 2 Invalid date time format : '1957009'
3 Component PID.9.7 2 Invalid table entry value : '3216' for table Name Type
4 Component PID.9.7 2 Value '3216' length (4) exceed limit (1)
5 Component PID.11.6 2 Invalid table entry value : 'INDIA' for table Country Code
6 Component PID.11.6 2 Value 'INDIA' length (5) exceed limit (3)
7 Field PID.12 2 Field should not contain component(s)
8 Component PID.18.1 2 Field required but has no value.
9 Field ORC.5 4 Invalid table entry value : 'D' for table Order status
10 Component ORC.7.4 4 Invalid date time format : '20103102300'
11 Component ORC.7.5 4 Invalid date time format : '216061102300'
12 Field ORC.15 4 Invalid date time format : '201606310230'
13 Field OBR.14 5 Invalid date time format : '201208056'
14 Field OBR.22 5 Invalid date time format : '201280058'
15 SubComponent OBR.27.1.1 5 Invalid numeric format : 'R'
16 Component OBR.27.4 5 Invalid date time format : '2012070957'
17 Component OBR.32.2 5 Invalid date time format : 'VRNEY,SCT'
但这并不能解释你的错误信息。您确定您阅读了消息文件并解析了内容吗?
您的代码有误。应该是
hl7.replace("\n", "\r")
如果要替换错误的段分隔符。