Skip to main content

csvImportFromFile

Type

command

Summary

Parse tabular data from a CSV file into an array

Syntax

csvImportFromFile <pFile>,<pOptions>,<rArray>

Description

This command fetches the content of the given file and parses it as comma separated values according to the options specified in pOptions. A structured array representing the parsed data is returned in rArray.

The quoting rule determines how quotes are treated in the data to import. If it is none then quotes have no effect on parsing, and the data is just split based on the chosen delimiter. Otherwise quoted values can contain the delimiter itself as well as return characters.

When specifying the quoting-rule for CSV import, consider the following options:

  • "all": All fields are quoted
  • "minimal": Fields with special characters (like delimiter) have quotes
  • "none": Quotes have no effect on parsing

Parameters

NameTypeDescription

pFile

The file containing csv data, either an absolute path or relative to the current default folder

pOptions

An array of options to use when importing csv. The keys are as follows:

  • "has-header-row": whether to treat the first row of data as column header names. Defaults to false.
  • "delimiter": character used to delimit values. Defaults to ','.
  • "quoting-rule": how to treat quoting in the target file; one of 'all', 'minimal', or 'none'.

rArray

Returns a nested array of data parsed from input file of format: { "headers": [ col1header, col2header,...], "data": [ [row1col1data, row1col2data, ...], [row2col1data, row2col2data, ...] ] }

Examples

local tFile
answer file "Select a csv file" with type "CSV files|csv|CSV"
put the result into tFile

local tArray
csvImportFromFile \
tFile, \
{ "has-header-row": true }, \
tArray