2

I am trying to parse hl7 files using hl7apy i am having below hl7 sample

sample :

MSH|^~\&|XXXX|C|PRIORITYHEALTH|PRIORITYHEALTH|20080511103530||ORU^R01|Q335939501T337311002|P|2.3|||
PID|1||94000000000^^^Priority Health||LASTNAME^FIRSTNAME^INIT||19460101|M|||||
PD1|1|||1234567890^PCPLAST^PCPFIRST^M^^^^^NPI|
OBR|1||185L29839X64489JLPF~X64489^ACC_NUM|JLPF^Lipid Panel - C||||||||||||1694^DOCLAST^DOCFIRST^^MD||||||20080511103529|||
OBX|1|NM|JHDL^HDL Cholesterol (CAD)|1|62|CD:289^mg/dL|>40^>40|""||""|F|||20080511103500|||^^^""|
OBX|2|NM|JTRIG^Triglyceride (CAD)|1|72|CD:289^mg/dL|35-150^35^150|""||""|F|||20080511103500|||^^^""|
OBX|3|NM|JVLDL^VLDL-C (calc - CAD)|1|14|CD:289^mg/dL||""||""|F|||20080511103500|||^^^""|
OBX|4|NM|JLDL^LDL-C (calc - CAD)|1|134|CD:289^mg/dL|0-100^0^100|H||""|F|||20080511103500|||^^^""|
OBX|5|NM|JCHO^Cholesterol (CAD)|1|210|CD:289^mg/dL|90-200^90^200|H||""|F|||20080511103500|||^^^""|

Code:

from hl7apy import parser
from hl7apy.exceptions import UnsupportedVersion

hl7 = open('sample.hl7', 'r').read()

try:
    m = parser.parse_message(hl7)
except UnsupportedVersion:
    m = parser.parse_message(hl7.replace("n", "r"))

  print(m.obx.obx_1.value)

but when i am trying to read OBX (Repeating segments) i am not getting any data, it is displaying nothing. What is am i doing wrong ?

animal
  • 994
  • 3
  • 13
  • 35
  • How do you read from the OBX segment? – sqlab Sep 30 '16 at 11:51
  • @sqlab i have updated my question please check. – animal Sep 30 '16 at 12:47
  • Have you tried `hl7`, rather than `hl7apy`? – Wayne Werner Sep 30 '16 at 12:56
  • 1
    I guess you have to take into consideration the groups **ORU_R01_ORDER_OBSERVATION and ORU_R01_OBSERVATION** – sqlab Sep 30 '16 at 13:17
  • @sqlab can you please give an example as it would help me understand well. – animal Sep 30 '16 at 14:08
  • @WayneWerner i didnt used hl7 if you have any samples or links can you please share. – animal Sep 30 '16 at 14:20
  • It seems my answer to your previous question regarding the wrong segment delimiter was correct. Why do you not honour it? – sqlab Oct 01 '16 at 07:46
  • @sqlab sorry for late reply. I accepted it. Please upvote if you find the question good. – animal Oct 03 '16 at 07:55
  • @animal I recently start working on hl7 files. Did you find any good way to parse the file or compare with other files? Thanks – mtkilic May 25 '17 at 17:45
  • @mtkilic i am using my own logic to parse the hl7 files. I am using python and Pig to parse the Hl7 files as the libraries were parsing it very slowly. – animal May 26 '17 at 09:48
  • @animal I am thinking about creating dictonary from hl7 segments, then convert that data frame using pandas. I am not sure if this is best exercise. [example](https://msarfati.wordpress.com/2015/06/20/python-hl7-v2-x-and-hl7apy-introduction-and-parsing-part-1/) – mtkilic May 26 '17 at 14:07
  • @mtkilic that's a good approcach. I have never used pandas though. – animal May 29 '17 at 06:58

1 Answers1

3

You have an error in your code. It should be

hl7.replace("\n", "\r")

You have to write all groups, if you want to access a field in (a) group(s)

try m.ORU_R01_RESPONSE.ORU_R01_ORDER_OBSERVATION.ORU_R01_OBSERVATION.OBX[0].obx_1.value for the first value of the first obx segment and so on.

I recommend to read Improve hl7apy documentation

sqlab
  • 6,412
  • 1
  • 14
  • 29
  • I want to parse(read) the groups, how will i do it. I am not trying to create any hl7 i want to read it. whatever you shared it seems for writing not reading. – animal Oct 03 '16 at 10:52
  • i am getting this error `hl7apy.exceptions.ChildNotFound: No child named ORU_R01_PATIENT_RESULT` – animal Oct 03 '16 at 11:31
  • That link solved my problem. Thanks. Please upvote the question if you liked it. – animal Oct 14 '16 at 07:14