Csv
libraryReads delimited text files (CSV) into Lua tables. The reader is deliberately simple: every line is split on the delimiter, quotes are not interpreted (a quoted value that contains the delimiter is split in two) and a line break inside a value is not supported. The first line is not treated specially, so a header row is just row 1. A leading byte order mark is removed and the text is read in the JVM default charset (UTF-8 on current Java versions).
- Kind Library, a global in every script
- Category Files & Documents
- Functions 2
Objects
Functions
-
readtable Reads a CSV file and returns all rows, including the header row, as lists of text values. -
readAddressListAddress Reads an address list from a CSV file and returns a helper that serves one random address at a time.
read()
Section titled “read()”Csv.read(fileLocation: string, delimiter: string)→table Reads a CSV file and returns all rows, including the header row, as lists of text values. Each value is trimmed. The delimiter is a regular expression, not a plain character: `;`, `,` and a tab (`"\t"`) work as expected, but a pipe must be written `"\\|"` (a bare `|` splits between every character). Empty cells at the end of a line are dropped, so rows can have different lengths, and an empty line becomes a row with one empty value. Throws `Failed to read csv` when the file cannot be read.
Parameters
-
fileLocationstring - CSV file to read, relative to the workspace
-
delimiterstring - Delimiter as a regular expression, for example `;` or `,` (required)
Returns
Example runs in CI
Fl.write("people.csv", "name;city\nAlice;Utrecht\nBob;Delft")local rows = Csv.read("people.csv", ";")print(rows[2][1], rows[2][2])readAddress()
Section titled “readAddress()”Csv.readAddress(fileLocation: string, delimiter: string)→ListAddress Reads an address list from a CSV file and returns a helper that serves one random address at a time. The file needs a header row (it is never picked) followed by lines of `street and number`, `zip code`, `city`. The delimiter is a regular expression, as in `Csv.read`. Meant for test data; see `ListAddress` for how the parts are extracted.
Parameters
-
fileLocationstring - CSV file to read, relative to the workspace
-
delimiterstring - Delimiter as a regular expression, for example `;`
Returns
Example runs in CI
Fl.write("addresses.csv", "street;zip;city\nMain Street 12a;1234 AB;Utrecht\nSide Road 5;5678 CD;Delft")local addresses = Csv.readAddress("addresses.csv", ";")print(addresses.street(), addresses.number(), addresses.zipcode(), addresses.city())