In [48]:
### DO NOT EDIT OR CHANGE THE CODE IN THIS DOCUMENT IN ANY WAY!!!!!

### READ ONLY


step_1 = open("Sample_redone.txt", "r")
step_2 = step_1.read()

In [183]:
step_3 = step_2.split(sep="/m")
step_3


Out[183]:
['MSH|^~\\&|CERNER||PriorityHealth||||ORU^R01|Q479004375T431430612|P|2.3',
 'PID|||001677980||SMITH^CURTIS||19680219|M||||||||||929645156318|123456789',
 'PD1||||1234567890^LAST^FIRST^M^^^^^NPI',
 'OBR|1|341856649^HNAM_ORDERID|000002006326002362|648088^Basic Metabolic Panel||',
 '20061122151600|||||||||1620^Hooker^Robert^L||||||20061122154733|||F|||||||||||20061122140000',
 'OBX|1|NM|GLU^Glucose Lvl|59|mg/dL|65-99^65^99|L|||F|||20061122154733/n']

In [184]:
step_4 = list(map(lambda x: x.split(sep="|"), step_3))
step_4


Out[184]:
[['MSH',
  '^~\\&',
  'CERNER',
  '',
  'PriorityHealth',
  '',
  '',
  '',
  'ORU^R01',
  'Q479004375T431430612',
  'P',
  '2.3'],
 ['PID',
  '',
  '',
  '001677980',
  '',
  'SMITH^CURTIS',
  '',
  '19680219',
  'M',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '929645156318',
  '123456789'],
 ['PD1', '', '', '', '1234567890^LAST^FIRST^M^^^^^NPI'],
 ['OBR',
  '1',
  '341856649^HNAM_ORDERID',
  '000002006326002362',
  '648088^Basic Metabolic Panel',
  '',
  ''],
 ['20061122151600',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '1620^Hooker^Robert^L',
  '',
  '',
  '',
  '',
  '',
  '20061122154733',
  '',
  '',
  'F',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '',
  '20061122140000'],
 ['OBX',
  '1',
  'NM',
  'GLU^Glucose Lvl',
  '59',
  'mg/dL',
  '65-99^65^99',
  'L',
  '',
  '',
  'F',
  '',
  '',
  '20061122154733/n']]

In [187]:
import numpy as np
step_5 = np.asarray(step_4)
list(range(1, len(step_5)))
step_5[2]

### break


Out[187]:
['PD1', '', '', '', '1234567890^LAST^FIRST^M^^^^^NPI']

In [172]:
r = (1,2,3,4,5)
p = (3, 4)
r.append(p)


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-172-0d9a47325f5c> in <module>()
      1 r = (1,2,3,4,5)
      2 p = (3, 4)
----> 3 r.append(p)

AttributeError: 'tuple' object has no attribute 'append'

In [211]:
def final_comp(y):
    mylist = []
    for x in range(0, ((len(y)-1))):
        temp = list(map(lambda x: x.split(sep="^"), (y[x])))

print(final_comp(step_5))


None

In [179]:
def final_comp3(y):
    (list(map(lambda x: x.split(sep="^"), (y[x])))) for x in y

print(final_comp3(step_5))


  File "<ipython-input-179-7f4c0104a6d9>", line 2
    (list(map(lambda x: x.split(sep="^"), (y[x])))) for x in y
                                                      ^
SyntaxError: invalid syntax

In [215]:
def final_comp2(y):
    for i in y:
        temp = list(map(lambda x: x.split(sep="^"), i))
        print(temp)

### Objects of objects, flat-map, or flatten the resulting objects into a single object
        
g = final_comp2(step_5)
print(g)


[['MSH'], ['', '~\\&'], ['CERNER'], [''], ['PriorityHealth'], [''], [''], [''], ['ORU', 'R01'], ['Q479004375T431430612'], ['P'], ['2.3']]
[['PID'], [''], [''], ['001677980'], [''], ['SMITH', 'CURTIS'], [''], ['19680219'], ['M'], [''], [''], [''], [''], [''], [''], [''], [''], [''], ['929645156318'], ['123456789']]
[['PD1'], [''], [''], [''], ['1234567890', 'LAST', 'FIRST', 'M', '', '', '', '', 'NPI']]
[['OBR'], ['1'], ['341856649', 'HNAM_ORDERID'], ['000002006326002362'], ['648088', 'Basic Metabolic Panel'], [''], ['']]
[['20061122151600'], [''], [''], [''], [''], [''], [''], [''], [''], ['1620', 'Hooker', 'Robert', 'L'], [''], [''], [''], [''], [''], ['20061122154733'], [''], [''], ['F'], [''], [''], [''], [''], [''], [''], [''], [''], [''], [''], ['20061122140000']]
[['OBX'], ['1'], ['NM'], ['GLU', 'Glucose Lvl'], ['59'], ['mg/dL'], ['65-99', '65', '99'], ['L'], [''], [''], ['F'], [''], [''], ['20061122154733/n']]
None

In [156]:
for i in step_5:
        list(map(lambda x: x.split(sep="^"), (i)))

In [126]:
### resume




step_5.split(sep="^")


---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-126-cba8ee8bb0f0> in <module>()
----> 1 step_5.split(sep="^")

AttributeError: 'numpy.ndarray' object has no attribute 'split'

In [52]:
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])))

In [201]:
step_7 = [step_6a, step_6b, step_6c, step_6d, step_6e, step_6f]
step_7[0]


Out[201]:
[['MSH'],
 ['', '~\\&'],
 ['CERNER'],
 [''],
 ['PriorityHealth'],
 [''],
 [''],
 [''],
 ['ORU', 'R01'],
 ['Q479004375T431430612'],
 ['P'],
 ['2.3']]

In [203]:
list_keys = list()
for x in range(len(step_8)):
    list_keys.append("Field" + str(x+0))
list_keys


Out[203]:
['Field0',
 'Field1',
 'Field2',
 'Field3',
 'Field4',
 'Field5',
 'Field6',
 'Field7',
 'Field8',
 'Field9',
 'Field10',
 'Field11']

In [210]:
step_9 = crazy_function_segment(step_7)
crazy_function_field(step_7[0])


Out[210]:
{'Field01': ['MSH'],
 'Field010': ['Q479004375T431430612'],
 'Field011': ['P'],
 'Field012': ['2.3'],
 'Field02': ['', '~\\&'],
 'Field03': ['CERNER'],
 'Field04': [''],
 'Field05': ['PriorityHealth'],
 'Field06': [''],
 'Field07': [''],
 'Field08': [''],
 'Field09': ['ORU', 'R01']}

In [ ]:


In [208]:
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

In [209]:
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

### zero based ordering ex. (01, 02, ..., 10, 11)

In [224]:
d= dict(zip(list_keys, step_8))
d


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-224-cc8463a463b0> in <module>()
      1 d= dict(zip(list_keys, step_8))
----> 2 d["Field[]"]

KeyError: 'Field[]'

In [ ]: