Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to split, re-arrange in order HL7 message

Posted on 2014-07-31
17
Medium Priority
?
487 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
13 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Simple Linear Regression
Suggested Courses

782 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