Solved

How to split, re-arrange in order HL7 message

Posted on 2014-07-31
17
406 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
  • 8
  • 4
17 Comments
 
LVL 45

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 45

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
 

Author Comment

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

no answers.
0
 
LVL 45

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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 45

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 45

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 45

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 45

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 45

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
The viewer will learn how to implement Singleton Design Pattern in Java.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

747 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now