In [2]:
### In this box, I open the file "Sample_redone.txt", which is the hl7 sample file
### with all the values put into one line with the appropiate "/m" placements. The
### "r" in the open function means read only.
import numpy as np ### For later use
step_1 = open("Sample_redone.txt", "r")
step_2 = step_1.read()
In [3]:
### In this box, I split the original message into segments by spliting
### anywhere there's an "/m" in the message
step_3 = step_2.split(sep="/m")
step_3
### Notice the result is a list with 6 items (each segment)
Out[3]:
In [4]:
### In this box, I seperated each segment into individual fields using
### "|" as the split indicator.
step_4 = list(map(lambda x: x.split(sep="|"), step_3))
step_4
### Notice the result is now a list of list. 6 lists are now organized
### into a larger list.
Out[4]:
In [5]:
### All I do in this box is take the result of step_4 (list of lists)
### and turn it into an array. (I needd to do this in order to run
### step 6)
step_5 = np.asarray(step_4)
step_5
### Notice the "np." I'm calling the package numpy which I imported earlier.
Out[5]:
In [6]:
### In order to run the next bit of code, understand that step_5 followed
### by a in brackets [#], means I'm running the code only on the #th
### component of the array. So step_5[1] would be the SECOND component
### of the array. (Remeber Python is a 0 based numbering system)
step_6a = list(map(lambda x: x.split(sep="^"), (step_5[0])))
step_6b = list(map(lambda x: x.split(sep="^"), (step_5[1])))
step_6c = list(map(lambda x: x.split(sep="^"), (step_5[2])))
step_6d = list(map(lambda x: x.split(sep="^"), (step_5[3])))
step_6e = list(map(lambda x: x.split(sep="^"), (step_5[4])))
step_6f = list(map(lambda x: x.split(sep="^"), (step_5[5])))
### Side-note: If anyone could create a function which completes
### step_6 and step_7 given an array of arrays of n length, that
### would move us towards solving a good part of this project.
In [7]:
### Now I combine the results od step 6 into one array.
step_7 = [step_6a, step_6b, step_6c, step_6d, step_6e, step_6f]
### Play around pulling different segments to check the work of the functions
### using the brackets. Ex. (step_7[0]) or (step_7[4]) ect. ->
# step_7[0]
# step_7[3]
# step_7[2]
In [8]:
### Notice the results of step_7 yeild an array of lists of arrays.
### Now segments, fields, and subcomponents are correctly ordered in the array.
### The next bit of code is my beginnings of turning step_7 into a
### dictionary, with key:value pairs. The key being its placement,
### ex. (Segment 1, Field 6, Subfield 2, ect.) and the value being the
### cooresponing value in the array. ex. ('MHS', 'Priority Health, ect.)
In [9]:
### Two functions I created to help turn this into a dictionary.
### function_segment(y) first creates a list of KEYS the size of y, then it
### binds this list to the values fed in the function for y. See what this
### produces when y is step_7
def function_segment(y):
temp_list = list()
for x in range(len(y)):
temp_list.append("Segment" + str(x+1))
new = dict(zip(temp_list, y))
return new
step_8 = function_segment(step_7)
step_8
### Notice each segment has been successfully named
Out[9]:
In [19]:
### This next function is literally exactly thesame as the last, it
### just names things "Field#" instead of "Segment#"
def function_field(y):
temp_list = list()
for x in range(len(y)):
temp_list.append("Field" + str(x+1))
new = dict(zip(temp_list, y))
return new
### Now I'm going to import each SEGMENT from step_8 into this function
### at a future date I will create a program to do this (and hopefully step_6)
### but for now, it's manual
step_9 = ((function_field(step_8['Segment1'])), (function_field(step_8['Segment2'])),
(function_field(step_8['Segment3'])), (function_field(step_8['Segment4'])),
(function_field(step_8['Segment5'])))
### And look at that! It erases the Segment numbers! Wonderful, well
### I'll work on fixing that up later. This is where I stop. I have
### a half a dozen boxes more of code throughout this that I'm working with,
### but I only wanted to include what was actually working. Add, subtract, or
### do whatever possible to make this better. Thanks guys!
In [20]:
### try number 2!
step_9b = ((function_field(step_7[0])), (function_field(step_7[1])),
(function_field(step_7[2])), (function_field(step_7[3])),
(function_field(step_7[4])), function_field(step_7[5]))
step_9c = function_segment(step_9b)
step_9c
Out[20]:
In [ ]: