-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhl7.py
92 lines (74 loc) · 2.96 KB
/
hl7.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# Description: This file contains a function that parses an HL7 file and writes the data to a CSV file.
# Author: Abdullahil Kafi
from itertools import zip_longest
from hl7apy import parser
from hl7apy.parser import parse_message
from hl7apy.core import Group, Segment
from hl7apy.exceptions import UnsupportedVersion
import csv
segments = []
segment_final = []
hl7 = open("test.hl7", "r").read()
try:
msg = parser.parse_message(hl7.replace('\n', '\r'), find_groups=True, validation_level=1)
except UnsupportedVersion:
msg = parser.parse_message(hl7.replace('\n', '\r'), find_groups=True, validation_level=2)
indent = " "
indent_seg = " "
indent_fld = " "
def subgroup (group, indent):
indent = indent + " "
print (indent , group)
segment_msg = []
for group_segment in group.children:
if isinstance(group_segment, Group):
subgroup (group_segment)
else:
print(indent_seg, indent ,group_segment)
seg = str(group_segment)
segments.append(seg)
for attribute in group_segment.children:
print(indent_fld, indent ,attribute, attribute.value)
segment_msg.append(attribute.value)
segment_final.append(segment_msg)
def showmsg(msg):
for segment in msg.children:
if isinstance(segment, Segment):
print (indent ,segment)
seg = str(segment)
segments.append(seg)
segment_msg = []
for attribute in segment.children:
print(indent_fld, indent, attribute, attribute.value)
segment_msg.append(attribute.value)
print(segment_msg)
segment_final.append(segment_msg)
print( 'final', segment_final)
if isinstance(segment, Group):
for group in segment.children:
print (indent,group)
for group_segment in group.children:
if isinstance (group_segment, Group):
subgroup (group_segment, indent)
else:
print(indent_seg, indent ,group_segment)
seg = str(group_segment)
segments.append(seg)
segment_msg = []
for attribute in group_segment.children:
print(indent_fld, indent, attribute, attribute.value)
segment_msg.append(attribute.value)
print(segment_msg)
segment_final.append(segment_msg)
print('final', segment_final)
showmsg(msg)
print(segments)
s = list(map(lambda elem: elem.replace('>', ''), segments))
s = list(map(lambda elem: elem.replace('<', ''), s))
print(s)
print(segment_final)
transposed_signals = list(zip_longest(*segment_final))
with open('test.csv', 'w') as f:
write = csv.writer(f)
write.writerow(s)
write.writerows(transposed_signals)