csv API reference
RFC 4180 comma-separated values: parse text into rows of fields and format rows back into text. Pure Jennifer - no Go, no system library. The delimiter is configurable, so the same code reads and writes TSV and other single-character-separated formats. Parsing is strict: a quote inside an unquoted field, text after a closing quote, an unclosed quote and records of different widths are csv errors. Records separate on CRLF, LF or a bare CR. format() quotes a field with " when it contains the delimiter, a quote or a line break, doubles an embedded quote, and joins records with CRLF without a trailing line break, so parse(format(rows)) returns the rows.
Import with import "csv.j" as csv;. See the csv guide for prose and examples.
Contents
| Name | What it is |
|---|---|
closeReader | Close a streaming reader (closes the underlying file handle). |
closeWriter | Close a streaming writer (closes the underlying file handle). |
dialect | A Dialect with the given delimiter and the defaults: quote ", no comment prefix, no trimming, every recor... |
format | Join rows into standard comma-delimited CSV. |
formatDialect | Format rows under a Dialect (delimiter, quote, width), like formatWith: CRLF between records, no trailing l... |
formatSafe | Injection-safe standard comma-delimited CSV (see formatSafeWith). |
formatSafeWith | Like formatWith, but neutralises spreadsheet formula injection (CWE-1236) before quoting: any field whose f... |
formatWith | Join rows into text with a single-character delimiter, quoting each field as needed. |
fromRecords | The inverse of toRecords: emit the header row followed by one row per record, taking fields in header order. |
parse | Parse standard comma-delimited CSV (see parseWith). |
parseDialect | Parse CSV text under a Dialect: custom delimiter and quote, comment-line skipping, optional trimming, and t... |
parseWith | Parse CSV text with a single-character delimiter and return its rows, each a list of string fields. |
readRow | Read the next record from the reader and return its fields. |
reader | Wrap an open, read-mode fs.File as a comma-delimited streaming reader. |
readerEof | Whether the reader has no record left. |
readerWith | Wrap an open, read-mode fs.File as a streaming reader with a custom single-character delimiter. |
toRecords | Treat the first row as a header and map each later row into a map of string to string keyed by the header... |
writeRow | Append one record to the writer: the fields, quoted as needed, followed by CRLF. |
writer | Wrap an open write/append-mode fs.File as a comma-delimited streaming writer. |
writerWith | Wrap an open write/append-mode fs.File as a streaming writer with a custom single-character delimiter. |
Dialect | struct: A CSV dialect: the field delimiter, the quote character, an optional comment-line prefix (a physical line w... |
Reader | struct: A streaming CSV reader over an open fs.File, for tables too large to hold in memory. |
Writer | struct: A streaming CSV writer over an open fs.File. |
Functions
closeReader
csv.closeReader(reader as Reader)Close a streaming reader (closes the underlying file handle).
reader- the reader
closeWriter
csv.closeWriter(writer as Writer)Close a streaming writer (closes the underlying file handle).
writer- the writer
dialect
csv.dialect(delimiter as string)A Dialect with the given delimiter and the defaults: quote ", no comment prefix, no trimming, every record as wide as the first. Set the other fields to customise.
delimiter- the single-character field delimiter
- Returns
Dialect - the dialect
format
csv.format(rows as list of list of string)Join rows into standard comma-delimited CSV. Not safe for a spreadsheet target - use formatSafe when the output may be opened in Excel / Sheets (see formatWith on the CWE-1236 formula-injection risk).
rows- the rows of fields to format
- Returns
string - the formatted comma-separated text
- Throws
Error - kind
"csv"as formatWith
formatDialect
csv.formatDialect(rows as list of list of string, d as Dialect)Format rows under a Dialect (delimiter, quote, width), like formatWith: CRLF between records, no trailing line break. A field that trimming would change and a first field starting with the comment prefix are quoted, so parseDialect with the same dialect returns the rows.
rows- the rows of fields to format
d- the dialect
- Returns
string - the formatted text
- Throws
Error - kind
"csv"on a bad dialect, a row with no fields, or a row of the wrong width
formatSafe
csv.formatSafe(rows as list of list of string)Injection-safe standard comma-delimited CSV (see formatSafeWith). The export-to-spreadsheet path; prefer it over format for spreadsheet targets.
rows- the rows of fields to format
- Returns
string - the formatted, injection-neutralised comma-separated text
formatSafeWith
csv.formatSafeWith(rows as list of list of string, delim as string)Like formatWith, but neutralises spreadsheet formula injection (CWE-1236) before quoting: any field whose first character is =, +, -, @, a tab, or a CR is prefixed with a single apostrophe so Excel / Google Sheets import it as literal text instead of executing it. Use this - not formatWith - for any CSV a spreadsheet will open.
rows- the rows of fields to format
delim- the single-character field delimiter
- Returns
string - the formatted, injection-neutralised text
formatWith
csv.formatWith(rows as list of list of string, delim as string)Join rows into text with a single-character delimiter, quoting each field as needed. Records are separated by CRLF (RFC 4180) with no trailing line break. A record that is one empty field is written "", so it reads back as a record. Every row must have as many fields as the first, and at least one.
Not safe for a spreadsheet target. A field beginning = + - @ (or a tab / CR) is a formula that Excel / Google Sheets will execute on open (CWE-1236). If the output is opened as a spreadsheet, use formatSafe / formatSafeWith, which neutralise such fields.
rows- the rows of fields to format
delim- the single-character field delimiter
- Returns
string - the formatted delimiter-separated text
- Throws
Error - kind
"csv"on a bad delimiter, a row with no fields, or rows of different widths
fromRecords
csv.fromRecords(
header as list of string,
records as list of map of string to string)The inverse of toRecords: emit the header row followed by one row per record, taking fields in header order. The explicit header fixes the column order, which map iteration does not.
header- the column names, in output order
records- the records to emit
- Returns
list of list of string - the header row followed by one row per record
- Throws
Error - kind
"csv"when the header names a column twice, or a record lacks a header key or has a key the header does not name
parse
csv.parse(s as string)Parse standard comma-delimited CSV (see parseWith).
s- the CSV text to parse
- Returns
list of list of string - the parsed rows of fields
- Throws
Error - kind
"csv"as parseWith
parseDialect
csv.parseDialect(text as string, d as Dialect)Parse CSV text under a Dialect: custom delimiter and quote, comment-line skipping, optional trimming, and the record width. Quoted fields keep their content (including whitespace and line breaks) verbatim.
text- the CSV text to parse
d- the dialect
- Returns
list of list of string - the parsed rows of fields
- Throws
Error - kind
"csv"on a bad dialect, malformed quoting (with its line and column) or a record of the wrong width
parseWith
csv.parseWith(s as string, delim as string)Parse CSV text with a single-character delimiter and return its rows, each a list of string fields. A quoted field may hold the delimiter, line breaks and doubled quotes; a blank line is a record with one empty field; an empty input yields no rows. Every record must have as many fields as the first.
s- the CSV text to parse
delim- the single-character field delimiter
- Returns
list of list of string - the parsed rows of fields
- Throws
Error - kind
"csv"on a bad delimiter, malformed quoting (with its line and column) or a record of a different width
readRow
csv.readRow(reader as Reader)Read the next record from the reader and return its fields. A quoted field may span lines; CRLF, LF and a bare CR end a record, as in parse, and a blank line is a record with one empty field [""]. Every record must have as many fields as the first. Returns [] once no record is left (guard the loop with readerEof). A malformed record is consumed before its error is thrown, so a caller that catches it can read on.
reader- the reader
- Returns
list of string - the record's fields, or
[]at the end - Throws
Error - kind
"csv"on malformed quoting (with its line and column) or a record of a different width
reader
csv.reader(file as fs.File)Wrap an open, read-mode fs.File as a comma-delimited streaming reader.
file- an open read-mode file handle
- Returns
Reader - the reader
readerEof
csv.readerEof(reader as Reader)Whether the reader has no record left. Loop while (not readerEof($r)).
reader- the reader
- Returns
bool - true once the file is exhausted and every record returned
readerWith
csv.readerWith(file as fs.File, delim as string)Wrap an open, read-mode fs.File as a streaming reader with a custom single-character delimiter.
file- an open read-mode file handle
delim- the single-character field delimiter
- Returns
Reader - the reader
- Throws
Error - kind
"csv"on a bad delimiter
toRecords
csv.toRecords(rows as list of list of string)Treat the first row as a header and map each later row into a map of string to string keyed by the header names. An empty input yields no records.
rows- the rows, with the first as the header
- Returns
list of map of string to string - one record per non-header row
- Throws
Error - kind
"csv"when the header names a column twice or a row does not have one field per header name
writeRow
csv.writeRow(writer as Writer, fields as list of string)Append one record to the writer: the fields, quoted as needed, followed by CRLF. A record that is one empty field is written "".
writer- the writer
fields- the record's fields
- Throws
Error - kind
"csv"when the record has no fields or not as many as the first; nothing is written then
writer
csv.writer(file as fs.File)Wrap an open write/append-mode fs.File as a comma-delimited streaming writer.
file- an open write- or append-mode file handle
- Returns
Writer - the writer
writerWith
csv.writerWith(file as fs.File, delim as string)Wrap an open write/append-mode fs.File as a streaming writer with a custom single-character delimiter.
file- an open write- or append-mode file handle
delim- the single-character field delimiter
- Returns
Writer - the writer
- Throws
Error - kind
"csv"on a bad delimiter
Structs
Dialect
A CSV dialect: the field delimiter, the quote character, an optional comment-line prefix (a physical line whose record starts with it is skipped on parse; "" disables comments), whether whitespace around fields is trimmed on parse, and the record width. Build one with dialect(delimiter) for the defaults.
def struct Dialect {
delimiter as string,
quote as string,
comment as string,
trim as bool,
width as int
};delimiter- the single-character field delimiter
quote- the quote character (default
") comment- the comment-line prefix, or "" for none
trim- trim whitespace from unquoted fields, and allow spaces and tabs around a quoted field (its content stays verbatim)
width- the fields every record must have: 0 (default) as many as the first record, a count, or -1 for any number
Reader
A streaming CSV reader over an open fs.File, for tables too large to hold in memory. The file and the read state are handles shared by every copy of the reader, so successive readRow calls advance the same stream. Build it with reader or readerWith.
def struct Reader {
file as fs.File,
delim as string,
state as channel of ReadState
};file- the underlying open file handle
delim- the single-character field delimiter
state- the read buffer and position (internal)
Writer
A streaming CSV writer over an open fs.File. Each writeRow call appends one quoted-as-needed record terminated by CRLF, so a large table is written without building the whole text in memory. Build it with writer or writerWith.
def struct Writer {
file as fs.File,
delim as string,
state as channel of WriteState
};file- the underlying open write/append-mode file handle
delim- the single-character field delimiter
state- the record width and count (internal)