Skip to content

Reads 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).

Functions

  • read table Reads a CSV file and returns all rows, including the header row, as lists of text values.
  • readAddress ListAddress Reads an address list from a CSV file and returns a helper that serves one random address at a time.
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

fileLocation string
CSV file to read, relative to the workspace
delimiter string
Delimiter as a regular expression, for example `;` or `,` (required)

Returns

table List of rows; each row is a list of strings

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])
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

fileLocation string
CSV file to read, relative to the workspace
delimiter string
Delimiter as a regular expression, for example `;`

Returns

ListAddress A random address from the list read by `Csv.readAddress`, split into street, number, number addition, zip code and city.

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())