Parse a CSV/TSV string into a list of rows. Each row is a list of string field values. Automatically strips a leading UTF-8 BOM and normalises all line-ending styles.
@eryx/data/csv Module
CSV reading and writing utilities.
Summary
Functions
API Reference
Functions
csv.parse
Parameters
-- The raw CSV text to parse.
-- Parsing options. See ParseOptions for available fields.
Returns
The parsed rows.
csv.generate
Serialize a list of rows into a CSV/TSV string. A line terminator is appended after every row, including the last
Parameters
-- List of rows, where each row is a list of string field values.
-- Generation options. See GenerateOptions for available fields.
Returns
The serialized CSV text.
csv.parseDict
Parse CSV input and return each row as a dictionary keyed by field name.
Field names come from the first data row unless opts.columns is set,
in which case the first row is treated as data. Missing fields are filled
with restval; extra fields beyond the header are discarded.
Parameters
-- The raw CSV text to parse.
-- Parsing options. See DictParseOptions for available fields.
Returns
csv.generateDict
Serialize dictionary rows into CSV, optionally prepending a header row
built from fieldnames. Each row is a table keyed by field name.
Parameters
-- Ordered column names that control output column order.
-- Generation options. See DictGenerateOptions for available fields.
Returns
The serialized CSV text.
csv.registerDialect
Register (or overwrite) a named dialect.
Parameters
csv.getDialect
Retrieve a named dialect table, or nil if not registered.
Parameters
-- The dialect name to look up.
Returns
Dialect?
csv.unregisterDialect
Remove a named dialect. No-op if the name is not registered.
Parameters
-- The dialect name to remove.
csv.listDialects
Return a sorted list of all registered dialect names.
Returns
{ string }
csv.sniff
Examine the first 20 non-empty lines of input and return a best-guess
dialect. The heuristic prefers the delimiter that produces a consistent
column count across all sample lines, breaking ties by column count.
Parameters
-- The raw CSV text to analyse.
-- Delimiter characters to consider. Defaults to { ",", "\t", "|", ";", ":" }.
Returns
SniffResult
Types
Dialect
Core format description shared between parsing and generation. Dialect presets contain only these fields -- parse- and generate-specific options live on their respective option types.
Field separator (single byte). Default ",".
Character used to quote fields (single byte). Default '"'.
Character used to escape special characters (single byte). Default nil (disabled).
Whether a quote character inside a quoted field is escaped by doubling it. Default true.
One of the csv.QUOTE_* constants controlling quoting behaviour. Default QUOTE_MINIMAL.
String appended after each row during generation. Default "\r\n".
ParseOptions
Options for csv.parse
Skip whitespace immediately following the delimiter. Default false.
Error on malformed input (bare quotes, unterminated fields, etc.). Default false.
Single-byte prefix that marks an entire line as a comment. Default nil (disabled).
Trim leading/trailing whitespace from unquoted fields. Default false.
Maximum number of characters allowed per field. Default nil (unlimited).
Discard rows that consist of a single empty field. Default false.
GenerateOptions
Options for csv.generate
DictParseOptions
Additional options for csv.parseDict, on top of ParseOptions.
Explicit column names. When set, the first row is treated as data, not a header.
Value used for missing fields when a row is shorter than the header. Default "".
DictGenerateOptions
Additional options for csv.generateDict, on top of GenerateOptions.
Value used for missing keys when a dict row lacks a field name. Default "".
Whether to emit a header row from the field names. Default true.
SniffResult
Result returned by csv.sniff.
The best-guess field delimiter.
The detected quote character, or nil if none was found.
Whether doubled-quote escaping is assumed.
Constants
- Quote only when the field contains special characters.
csv.QUOTE_MINIMAL=0- Quote every field unconditionally.
csv.QUOTE_ALL=1- Quote non-numeric fields; on read, reject unquoted non-numeric in strict mode.
csv.QUOTE_NONNUMERIC=2- Never quote; use `escapechar` to protect special characters.
csv.QUOTE_NONE=3