Skip to content
Jennifer Programming Language

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

NameWhat it is
closeReaderClose a streaming reader (closes the underlying file handle).
closeWriterClose a streaming writer (closes the underlying file handle).
dialectA Dialect with the given delimiter and the defaults: quote ", no comment prefix, no trimming, every recor...
formatJoin rows into standard comma-delimited CSV.
formatDialectFormat rows under a Dialect (delimiter, quote, width), like formatWith: CRLF between records, no trailing l...
formatSafeInjection-safe standard comma-delimited CSV (see formatSafeWith).
formatSafeWithLike formatWith, but neutralises spreadsheet formula injection (CWE-1236) before quoting: any field whose f...
formatWithJoin rows into text with a single-character delimiter, quoting each field as needed.
fromRecordsThe inverse of toRecords: emit the header row followed by one row per record, taking fields in header order.
parseParse standard comma-delimited CSV (see parseWith).
parseDialectParse CSV text under a Dialect: custom delimiter and quote, comment-line skipping, optional trimming, and t...
parseWithParse CSV text with a single-character delimiter and return its rows, each a list of string fields.
readRowRead the next record from the reader and return its fields.
readerWrap an open, read-mode fs.File as a comma-delimited streaming reader.
readerEofWhether the reader has no record left.
readerWithWrap an open, read-mode fs.File as a streaming reader with a custom single-character delimiter.
toRecordsTreat the first row as a header and map each later row into a map of string to string keyed by the header...
writeRowAppend one record to the writer: the fields, quoted as needed, followed by CRLF.
writerWrap an open write/append-mode fs.File as a comma-delimited streaming writer.
writerWithWrap an open write/append-mode fs.File as a streaming writer with a custom single-character delimiter.
Dialectstruct: A CSV dialect: the field delimiter, the quote character, an optional comment-line prefix (a physical line w...
Readerstruct: A streaming CSV reader over an open fs.File, for tables too large to hold in memory.
Writerstruct: A streaming CSV writer over an open fs.File.

Functions

closeReader

jennifer
csv.closeReader(reader as Reader)

Close a streaming reader (closes the underlying file handle).

reader
the reader

closeWriter

jennifer
csv.closeWriter(writer as Writer)

Close a streaming writer (closes the underlying file handle).

writer
the writer

dialect

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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

jennifer
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.

jennifer
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.

jennifer
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.

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