The csv
statement is used to create and populate CSV files. It is typically combined with foreach loops to write values extracted from an array in a JSON and/or XML document stored in a named buffer.
CSV files are produced via the use of multiple csv
statements which perform the following functions:
Create a new empty CSV file
Define the headers
Finalise the headers
Write data to one or more rows of the file
Close the file
All CSV files created by the csv
command use a comma - ,
- as the separator character and a double quote - "
- as the quote character. Headers and data fields are automatically separated and quoted.
The following is used to create a new, empty CSV file:
csv
label = filename
The label must not be associated with any other open CSV file. Up to 16 CSV files may be open simultaneously and the label is used by suesequent csv
statements to determine which of the open files the statement should operate on. Labels are case sensitive and may be from 1 to 15 characters in length.
The specified filename is created immediately, and if it is the name of an existing file then it will be truncated to 0 bytes when opened.
The filename argument may contain a path component but the csv
statement does not create directories, so any path component in the filename must already exist. The path, if specified, will be local to the Exivity home directory.
csv usage = "${exportdir}/azure_usage.csv"
This section refers to add_headers
as the action, but either add_header
or add_headers
may be used. Both variants work in an identical fashion.
csv add_headers
label header1 [header2 ... headerN]
All CSV files created by USE script must start with a header row which names the columns in the file. The number of columns can vary from file to file, but in any given file every data row must have the same number of columns as there are headers.
To create one or more columns in a newly created CSV file, the csv add_headers
statement is used as shown above. The label must match the label previously associated with the file as described previously.
One or more header names can be specified as arguments to csv add_headers
. Multiple instances of the csv add_headers
statement may reference the same CSV file, as each statement will append additional headers to any headers already defined for the file.
No checks are done to ensure the uniqueness of the headers. It is therefore up to the script author to ensure that all the specified headers in any given file are unique.
csv add_headers usage username user_id subscription_id
This section refers to fix_headers
as the action, but either fix_header
or fix_headers
may be used. Both variants work in an identical fashion
csv fix_headers
label
After csv add_headers
has been used to define at least one header, the headers are finalised using csv fix_headers
statement. Once the headers have been fixed, no further headers can be added to the file and until the headers have been fixed, no data can be written to the file.
csv fix_headers usage
This section refers to write_fields
as the action, but either write_field
or write_fields
may be used. Both variants work in an identical fashion
csv write_fields
label value1 [value2 ... valueN]
After the headers have been fixed, the csv write_fields
statement is used to write one or more fields of data to the CSV file. Currently it is not possible to write a blank field using csv write_fields
, however when extracting data from a buffer using a parslet, if the extracted value is blank then it will automatically be expanded to the string (no value)
.
USE keeps track of the rows and columns as they are populated using one or more csv write_fields
statements, and will automatically write the fields from left to right starting at the first column in the first data row and will advance to the next row when the rightmost column has been written to.
It is the responsibility of the script author to ensure that the number of fields written to a CSV file is such that when the file is closed, the last row is complete, in order to avoid malformed files with one or more fields missing from the last row.
csv close
label
Once all fields have been written to a CSV file, it must be closed using the csv close
statement. This will ensure that all data is properly flushed to disk, and will free the label for re-use.
csv close usage
Consider the file "\examples\json\customers.json" representing two customers:
Using a combination of foreach loops and parslets, the information in the above JSON can be converted to CSV format as follows:
The resulting CSV file is as follows: