In this article we will show you how to parse CSV data files.
This code assumes that you are using a From Translator code to read your CSV files and push them onto the Iguana queue for processing (this is not addressed in this tutorial).
We recommend that you type in the code for each step, but we also include complete Sample Code if you prefer to paste it in and follow along.
Create the Channel [top]
- Create a Channel with the the following settings:
- Source = From Translator
- Destination = To Translator
- Channel name = Parse CSV File
- Click the Add Channel button to create the channel.
Ignore the red warning messages, see resolving the milestone configuration error. - Open the Translator by clicking the Edit Script link at the bottom of the Destination tab.
- Download and import the Parse_CSV_File_To_Translator.zip project file.
This file contains a skeleton project and 12 CSV sample messages. - Iguana will load the project and data into the Translator, your screen should look like this:
Tutorial Instructions [top]
- Pass the message data to the script.
- Iguana automatically passes the message data to the
main()
function - The message can be accessed using the
Data
parameter - No action is needed
- Iguana automatically passes the message data to the
- Parse the CSV message into a table.
Add the following line of code to your script:
- Inspect the parsed data by clicking on the table annotation:
- Navigate through the other 11 sample CSV messages
- The first 9 messages contain valid CSV data
- The 10th message contains invalid data and raises an error
- The 11th and 12th messages use different delimiters
- Fix the error in the 10th message
- View the error:
- We can see that the issue is the un-escaped single quotes (“) before and after Speedie that both need to be escaped by changing them to two quotes (“”)
- Check the message data to be sure, click on the Data annotation to view the message:
- Viewing the message data confirms that we indeed have two un-escaped quotes (“) within a quoted field:
- Edit the current sample message by clicking the Edit Message icon on the toolbar:
- Escape the quotes around Speedie by changing them from single (“) to double (“”), then click Save.
Speedie before editing:
Speedie after editing:
- Now we are back in the Editor and the parsing works correctly:
- The correction we made means that our 9th and 10th messages are now exactly the same, you can confirm this be inspecting the messages using the sample data management screen.
- View the error:
- Parse the 11th message correctly by recognizing the tab delimiters
- Add the following line of code:
- View both parse results by clicking on their respective table annotations:
- Compare the two annotations, to confirm that the parse is working:
- Add the following line of code:
- Parse the 11th message correctly by recognizing the bar “|” delimiters
- Add the following line of code:
- Compare the annotations, to confirm that the parse is working (as we did above).
- Add the following line of code:
Complete Sample Code [top]
Here is the completed CSV parsing code that you can cut and paste into your script:
local csv = require 'csv' function main(Data) -- 1) Parse a comma separated CSV file local Csv = csv.parseCsv(Data) -- comma separated (default) -- 2) Parse a tab separated file --local Csv = csv.parseCsv(Data, '\t') -- tab separated (sample message 11) -- 3) Parse a bar "|" separated file --local Csv = csv.parseCsv(Data, '|') -- bar separated (sample message 12) trace(Csv) -- Examples of what you can do: -- a) Use in a To Translator and add code -- for saving patients to a database -- b) Use in a Filter Component and map to -- XML/JSON/HL7/etc then queue for further processing end
More Information [top]
- Parsing CSV Files goes into more detail
- See our other general tutorials in this section
- See our interface tutorials section
- See our code samples section