Namespace: joker.csv
v1.0Contents
Summary
Reads and writes comma-separated values (CSV) using RFC 4180-style parsing and formatting.
Index
Constants
Constants are variables with :const true in their metadata. Joker currently does not recognize them as special; as such, it allows redefining them or their values.-
(None.)
Variables
-
(None.)
Functions, Macros, and Special Forms
-
csv-seq
Function v1.0(csv-seq rdr)(csv-seq rdr opts)Returns records from rdr as a lazy sequence of string vectors.
rdr must be a string or implement io.Reader. Parsing happens as the returned
sequence is realized, so malformed input may throw Error later while the
sequence is consumed rather than when csv-seq is called.
opts may have the following keys:
:comma - field delimiter (defaults to ',').
Must be a valid char and must not be \r, \n,
or the Unicode replacement character (0xFFFD).
:comment - comment character (defaults to 0 meaning no comments).
Lines beginning with the comment character without preceding whitespace are ignored.
With leading whitespace the comment character becomes part of the
field, even if trim-leading-space is true.
comment must be a valid char and must not be \r, \n,
or the Unicode replacement character (0xFFFD).
It must also not be equal to comma.
:fields-per-record - number of expected fields per record.
If fields-per-record is positive, csv-seq requires each record to
have the given number of fields. If fields-per-record is 0 (default), csv-seq sets it to
the number of fields in the first record, so that future records must
have the same field count. If fields-per-record is negative, no check is
made and records may have a variable number of fields.
:lazy-quotes - if true, a quote may appear in an unquoted field and a
non-doubled quote may appear in a quoted field. Default value is false.
:trim-leading-space - if true, leading white space in a field is ignored.
This is done even if the field delimiter, comma, is white space.
Default value is false.
Example:
(joker.csv/csv-seq "a,b\nc,d")
;; => (["a" "b"] ["c" "d"]) -
write
Function v1.0(write f data)(write f data opts)Writes data as CSV to f and returns nil.
f must implement io.Writer, for example a file returned by joker.os/create.
data and opts have the same meaning as in joker.csv/write-string. write does
not close f. Throws Error for write failures, including errors reported while
flushing buffered CSV output. -
write-string
Function v1.0(write-string data)(write-string data opts)Formats data as CSV and returns the resulting string.
data must be Seqable, and each record within it must be Seqable. Field values
are converted to strings before writing.
opts may have the following keys:
:comma - field delimiter (defaults to ',').
:use-crlf - if true, uses \r\n as the line terminator. Default value is false.
Example:
(joker.csv/write-string [["a" "b"] ["c" "d"]])
;; => "a,b\nc,d\n"