I am using hl7apy to create a python script to generate hl7 messages with the data from a pipe-delimited file.
My input file looks like this
John|Doe|Facility
Smith|Doe1|Test_Facility
I have taken the example from hl7apy library and made some changes to read from a pipe delimited file. In the code, I am assigning the value of the second field in the file to PID.5.1 segment and expect to output two messages as I have two lines in my input file.
Code
from hl7apy.core import Message
from hl7apy.parser import parse_message
fileHandle = open('test_pipe.txt', 'r')
for line in fileHandle:
fields = line.split('|')
m = Message("ADT_A01")
m.msh.msh_3 = 'GHH_ADT'
m.msh.msh_7 = '20080115153000'
m.msh.msh_9 = 'ADT^A01^ADT_A01'
m.msh.msh_10 = "0123456789"
m.msh.msh_11 = "P"
m.msh.msh_12 = ""
m.msh.msh_16 = "AL"
m.evn.evn_2 = m.msh.msh_7
m.evn.evn_4 = "AAA"
m.evn.evn_5 = m.evn.evn_4
m.pid.pid_5.pid_5_1 = fields[1]
m.nk1.nk1_1 = '1'
m.nk1.nk1_2 = 'NUCLEAR^NELDA^W'
m.nk1.nk1_3 = 'SPO'
m.nk1.nk1_4 = '2222 HOME STREET^^ANN ARBOR^MI^^USA'
print (m.value)
fileHandle.close()
When I run this code, I am getting only one message in the output with the value of the second field (Doe1) in the second row instead of two messages in the output with the second field's value in both the rows. How do I make this code read all the rows in the file?
It's because fields
is being overwritten in the for loop.
You can probably change
for line in fileHandle:
fields = line.split('|')
to
fields = [line.split('|') for line in fileHandle]
Or you can change the indent of the rest of your code
for line in fileHandle:
fields = line.split('|')
m = Message("ADT_A01")
m.msh.msh_3 = 'GHH_ADT'
m.msh.msh_7 = '20080115153000'
m.msh.msh_9 = 'ADT^A01^ADT_A01'
m.msh.msh_10 = "0123456789"
m.msh.msh_11 = "P"
m.msh.msh_12 = ""
m.msh.msh_16 = "AL"
m.evn.evn_2 = m.msh.msh_7
m.evn.evn_4 = "AAA"
m.evn.evn_5 = m.evn.evn_4
m.pid.pid_5.pid_5_1 = fields[1]
m.nk1.nk1_1 = '1'
m.nk1.nk1_2 = 'NUCLEAR^NELDA^W'
m.nk1.nk1_3 = 'SPO'
m.nk1.nk1_4 = '2222 HOME STREET^^ANN ARBOR^MI^^USA'
print (m.value)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.