Skip to main content

csvImport

Type

command

Summary

Parse tabular data from a text into an array

Syntax

csvImport <pText>,<pOptions>,<rArray>

Description

This command takes in a string, computes the data based on optional parameters and returns it as an array

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

pText

Text to be parsed for data

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 text; one of 'all', 'minimal', or 'none'.

rArray

Returns a nested array with data parsed from input text with 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 tText
put url ("binfile:" & tFile) into tText

local tArray
csvImport \
tText, \
{ "has-header-row": true }, \
tArray