简体   繁体   English

使用python逐行解析hl7消息

[英]Parsing hl7 message line by line using python

i want to read my hl7 messages from a file line by line and parse them using python.我想逐行从文件中读取我的 hl7 消息并使用 python 解析它们。 I'm able to read But my problem is in parsing.我能够阅读但我的问题在于解析。 It parses only my 1st line of the file and prints till the 2nd line but does not parses futhur because it tells that my 2nd line is not hl7.它只解析文件的第一行并打印到第二行,但不会继续解析,因为它告诉我第二行不是 hl7。 And the error shown is显示的错误是

h=hl7.parse(line)
File "C:\Python27\lib\site-packages\hl7\parser.py", line 45, in parse
plan = create_parse_plan(strmsg, factory)
File "C:\Python27\lib\site-packages\hl7\parser.py", line 88, in create_parse_plan
assert strmsg[:3] in ('MSH')
AssertionError

for the code:对于代码:

with open('example.txt','r') as f:
    for line in f:
       print line
       print hl7.isfile(line)
       h=hl7.parse(line)

So how do i make my file a valid one.那么如何使我的文件成为有效文件。 This is example.txt file这是 example.txt 文件

MSH|^~\&|AcmeMed|Lab|Main HIS|St.Micheals|20130408031655||ADT^A01|6306E85542000679F11EEA93EE38C18813E1C63CB09673815639B8AD55D6775|P|2.6|
EVN||20050622101634||||20110505110517|
PID|||231331||Garland^Tracy||19010201|F||EU|147 Yonge St.^^LA^CA^58818|||||||28-457-773|291-697-644|
NK1|1|Smith^Sabrina|Second Cousin|
NK1|2|Fitzgerald^Sabrina|Second Cousin|
NK1|3|WHITE^Tracy|Second Cousin|
OBX|||WT^WEIGHT||78|pounds|
OBX|||HT^HEIGHT||57|cm|

I had a similar issue and came up with this solution that works for me.我遇到了类似的问题,并提出了适合我的解决方案。

In short, put all your lines into an object and then parse that object.简而言之,将所有行放入一个对象中,然后解析该对象。 (Obviously you can clean up the way I checked to see if the object is made yet or not, but I was going for an easy to read example.) (显然,您可以清理我检查对象是否已创建的方式,但我想要一个易于阅读的示例。)

a = 0
with open('example.txt','r') as f:
    for line in f:
       if a == 0:
           message = line
           a = 1
       else:
           message += line
h=hl7.parse(message)

Now you will have to clean up some \r\n depending on how the file is encoded for end of the line values.现在您将不得不清理一些 \r\n ,具体取决于文件是如何为行尾值编码的。 But it takes the message as valid and you can parse to your hearts content.但它将消息视为有效,您可以根据自己的喜好进行解析。

for line in h:
    print(line)

MSH|^~\&|AcmeMed|Lab|Main HIS|St.Micheals|20130408031655||ADT^A01|6306E85542000679F11EEA93EE38C18813E1C63CB09673815639B8AD55D6775|P|2.6|
EVN||20050622101634||||20110505110517|
PID|||231331||Garland^Tracy||19010201|F||EU|147 Yonge St.^^LA^CA^58818|||||||28-457-773|291-697-644|
NK1|1|Smith^Sabrina|Second Cousin|
NK1|2|Fitzgerald^Sabrina|Second Cousin|
NK1|3|WHITE^Tracy|Second Cousin|
OBX|||WT^WEIGHT||78|pounds|
OBX|||HT^HEIGHT||57|cm|

Tagging onto @jtweeder answer, the following code worked for prepping my HL7 data.标记@jtweeder 答案,以下代码用于准备我的 HL7 数据。

In notepad++, I noticed that each line ended with LF, but did not have CR.在 notepad++ 中,我注意到每一行都以 LF 结尾,但没有 CR。 It seems as though this hl7 library requires \r, not \n.似乎这个 hl7 库需要 \r,而不是 \n。

filename = "TEST.dat"  
lines = open(filepath + filename, "r").readlines()  
h = '\r'.join(lines)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM