Solved

How to split, re-arrange in order HL7 message

Posted on 2014-07-31
17
415 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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
 
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

ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

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.
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

803 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