-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_1.py
48 lines (42 loc) · 1.87 KB
/
parser_1.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
# Description: This file contains a function that parses an HL7 file and writes the data to a CSV file.
# Author: Abdullahil Kafi
import sys
from hl7apy.parser import parse_segment
import csv
def parse_hl7_to_csv(hl7_file_path, csv_file_path):
data = {
'Patient Info': [],
'Encounters': [],
'Lab Reports': [],
}
with open(hl7_file_path, 'r') as file:
for line in file:
hl7_content = line.replace('\n', '\r')
try:
segment = parse_segment(hl7_content)
if segment.name == 'PID':
patient_info = {field.name: field.value for field in segment.children if field.value is not None}
data['Patient Info'].append(patient_info)
elif segment.name == 'PV1':
encounter_info = {field.name: field.value for field in segment.children if field.value is not None}
data['Encounters'].append(encounter_info)
elif segment.name == 'OBX':
lab_info = {field.name: field.value for field in segment.children if field.value is not None}
data['Lab Reports'].append(lab_info)
except Exception as e:
print("Error parsing HL7 segment:", e)
# Write data to CSV
with open(csv_file_path, 'w', newline='') as csvfile:
writer = csv.writer(csvfile)
for segment_type, segments in data.items():
writer.writerow([segment_type])
for segment in segments:
writer.writerow(segment.values())
writer.writerow([])
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: python parser_1.py <input.hl7> <output.csv>")
sys.exit(1)
hl7_file_path = sys.argv[1]
csv_file_path = sys.argv[2]
parse_hl7_to_csv(hl7_file_path, csv_file_path)