Solved

How to split, re-arrange in order HL7 message

Posted on 2014-07-31
17
450 Views
Last Modified: 2014-08-17
I receive an HL7 message which looks like:
MSH|fdsas43423|fdsfs|432423|fdfads|423423|
PID|43243|fdfds|654|HGDF|76554|HGDS
PV1|5434|gfsd|543534|hgdfz|
ORC||||F|RE
OBR|20140709|gffsd|425643|hgd|543
OBX|65465|GFFSD|7664|aHGFF|4654
NTE|GHFDSGF|543|GFSFD|654|HGDF
OBX|423|FDSAD|432423|FDAS|423423
ORC||||p|RE
OBR|20140706|gffsd|425643|hgd|543
OBX|65465|GFFSD|7664|aHGFF|4654
NTE|GHFDSGF|543|GFSFD|654|HGDF
OBX|423|FDSAD|432423|FDAS|423423
ORC||||F|RE
OBR|20140710|gffsd|425643|hgd|543
OBX|65465|GFFSD|7664|aHGFF|4654
NTE|GHFDSGF|543|GFSFD|654|HGDF
OBX|423|FDSAD|432423|FDAS|423423
ORC||||F|RE
OBR|20140711|gffsd|425643|hgd|543
OBX|65465|GFFSD|7664|aHGFF|4654
NTE|GHFDSGF|543|GFSFD|654|HGDF
OBX|423|FDSAD|432423|FDAS|423423

I need to split the message into sections which start at ORC and end at the next one.  the field next to OBR is a timestamp which is alwasy unique and once the message is split into sections it needs to be rebuilt in order of the time stamp from latest to earliest.  

So it should look like:
MSH|fdsas43423|fdsfs|432423|fdfads|423423|
PID|43243|fdfds|654|HGDF|76554|HGDS
PV1|5434|gfsd|543534|hgdfz|
ORC||||p|RE
OBR|20140706|gffsd|425643|hgd|543
OBX|65465|GFFSD|7664|aHGFF|4654
NTE|GHFDSGF|543|GFSFD|654|HGDF
OBX|423|FDSAD|432423|FDAS|423423
ORC||||F|RE
OBR|20140709|gffsd|425643|hgd|543
OBX|65465|GFFSD|7664|aHGFF|4654
NTE|GHFDSGF|543|GFSFD|654|HGDF
OBX|423|FDSAD|432423|FDAS|423423
ORC||||F|RE
OBR|20140710|gffsd|425643|hgd|543
OBX|65465|GFFSD|7664|aHGFF|4654
NTE|GHFDSGF|543|GFSFD|654|HGDF
OBX|423|FDSAD|432423|FDAS|423423
ORC||||F|RE
OBR|20140711|gffsd|425643|hgd|543
OBX|65465|GFFSD|7664|aHGFF|4654
NTE|GHFDSGF|543|GFSFD|654|HGDF
OBX|423|FDSAD|432423|FDAS|423423

Thanks.
0
Comment
Question by:Nick Malone
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
17 Comments
 
LVL 46

Expert Comment

by:aikimark
ID: 40234004
It might be more helpful if you posted an example with different data in each 'section'.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40234212
Don't use homebrewed techniques - there are parsers available,  e.g. http://hl7api.sourceforge.net/
0
 
LVL 46

Expert Comment

by:aikimark
ID: 40234263
Since you are using Python, you might want to consider a Python-centric parser, such as hl7apy.
http://sourceforge.net/projects/hl7apy/
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:Nick Malone
ID: 40235339
I've requested that this question be deleted for the following reason:

no answers.
0
 
LVL 46

Expert Comment

by:aikimark
ID: 40235340
You have seen two different potential solutions and received a request to clarify your data.  Your "no answers" assertion is bogus.  I object to your deletion request.  Please work with the participating experts.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40235430
Your "no answers" assertion is bogus.  I object to your deletion request.

.. as do I
0
 
LVL 46

Expert Comment

by:aikimark
ID: 40237041
The following seems to work in my Windows environment using your posted data.  Be aware that if your posted data does not strongly resemble your actual/production data, this code will not work.
import os

def main():
    #!/usr/local/bin/python2.7
    os.chdir( "\\users\\aikimark\\downloads")
    f = open('HL7input.txt', 'r')

    groups = []
    agroup=[]
    groupkey=''
    recnum=0
    for line in f:
      a=line.split('|')
      if a[0]=='ORC':
          if len(agroup) == 1:
            #add to groups and reset
            agroup.append(recnum-1)
            agroup.append(groupkey)
            groups.append(agroup)
            agroup=[]
            groupkey=''
            agroup.append(recnum)
          else:
            agroup.append(recnum)

      elif a[0]=='OBR':
          #retain group key
          groupkey=a[1]
      recnum=recnum+1

    #add last group to the groups list
    agroup.append(recnum-1)
    agroup.append(groupkey)
    groups.append(agroup)
    firstgroupstart = groups[0][0]

##    print 'before sort'
##    for g in groups:
##      print g

    #sort on third item in tuple -- the date of the group in yyyymmdd format string
    sgroups=sorted(groups, key=lambda g: g[2])

##    print 'after sort'
##    for g in sgroups:
##      print g
    f.close
    f = open('HL7input.txt', 'r')
    linelist = f.readlines()
    f.close
    f = open('HL7output.txt', 'w')

    #print 'sorted file'
    print range(firstgroupstart)
    for l in range(firstgroupstart):
      f.write( linelist[l],)
    for g in sgroups:
      for outline in range(g[0],g[1]+1):
        f.write( linelist[outline],)
    f.close

if __name__ == '__main__':
    main()

Open in new window

0
 
LVL 46

Accepted Solution

by:
aikimark earned 500 total points
ID: 40237568
It bothered me that I was reading the file twice, so I streamlined the code.
import os

def main():
    #!/usr/local/bin/python2.7
    os.chdir( "\\users\\aikimark\\downloads")
    f = open('HL7input.txt', 'r')
    linelist = f.readlines()
    f.close

    groups = []
    agroup=[]
    groupkey=''
    recnum=0

    for line in linelist:
      a=line.split('|')
      if a[0]=='ORC':
          if len(agroup) == 1:
            #add to groups and reset
            agroup.append(recnum-1)
            agroup.append(groupkey)
            groups.append(agroup)
            agroup=[]
            groupkey=''
            agroup.append(recnum)
          else:
            agroup.append(recnum)

      elif a[0]=='OBR':
          #retain group key
          groupkey=a[1]
      recnum=recnum+1

    #add last group to the groups list
    agroup.append(recnum-1)
    agroup.append(groupkey)
    groups.append(agroup)
    firstgroupstart = groups[0][0]

##    print 'before sort'
##    for g in groups:
##      print g

    #sort on third item in tuple -- the date of the group in yyyymmdd format string
    sgroups=sorted(groups, key=lambda g: g[2])

##    print 'after sort'
##    for g in sgroups:
##      print g
    f = open('HL7output.txt', 'w')

    #print 'sorted file'
    print range(firstgroupstart)
    for l in range(firstgroupstart):
      f.write( linelist[l])
    for g in sgroups:
      for outline in range(g[0],g[1]+1):
        f.write( linelist[outline])
    f.close

Open in new window

0
 
LVL 46

Expert Comment

by:aikimark
ID: 40265656
accept http:#a40237568 as the solution
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40265884
Points splits
0
 
LVL 46

Expert Comment

by:aikimark
ID: 40265997
@CEHJ

Your proposed solution only went so far as the parsing.  It didn't address the sorting.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 40266006
a. that's why a points split
b. use of a proper API means that regular Java Collection sorting is trivial
0
 
LVL 46

Expert Comment

by:aikimark
ID: 40266028
a. although we both proposed a parsing library via a link to different sourceforge projects, I actually posted Python code that did the parsing without any extra parsing libraries.  My comment is a complete solution.
b. Java collection sorting is not a consideration with this Python problem.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question