Read and write CSV/TSV. Portable: the parser and writer are plain Clojure operating on strings and vectors, so they work identically in Clojure and ClojureScript. Clojure also gets convenience fns for reading from files, classpath resources, and urls (anything clojure.java.io/reader accepts), and for writing to files.
Two shapes in and out:
Synthesized from the various copies of this floating around (voracious, traverse): numeric coercion, :__id for a blank header, and BOM-stripping on file reads all came from there. Their :quote? auto-quote-numeric-looking-strings-on-write trick was dropped -- it was marked broken in both sources and rows->str's ordinary auto-quoting covers real needs.
Read and write CSV/TSV. Portable: the parser and writer are plain Clojure operating on strings and vectors, so they work identically in Clojure and ClojureScript. Clojure also gets convenience fns for reading from files, classpath resources, and urls (anything clojure.java.io/reader accepts), and for writing to files. Two shapes in and out: - raw: a seq of rows, each row a vector of string fields (first row usually a header). Always plain strings, untouched. - ms (mapseq): a seq of maps, keyed from the header row -- see hyperphor.multitool.data. Values are lightly coerced (empty field -> nil, otherwise numeric strings -> numbers) since that's almost always what you want out of a mapseq; pass :strings? true to keep everything as strings. Synthesized from the various copies of this floating around (voracious, traverse): numeric coercion, :__id for a blank header, and BOM-stripping on file reads all came from there. Their :quote? auto-quote-numeric-looking-strings-on-write trick was dropped -- it was marked broken in both sources and rows->str's ordinary auto-quoting covers real needs.
(header->key s)Default header field -> map key: a blank header (eg an unlabeled index column) becomes :__id, otherwise punctuation is replaced to make a valid keyword -- see core/keyword-safe.
Default header field -> map key: a blank header (eg an unlabeled index column) becomes :__id, otherwise punctuation is replaced to make a valid keyword -- see core/keyword-safe.
(ms->rows ms & {:keys [columns]})Convert mapseq ms into rows with a header row. :columns (optional) gives explicit
field order; defaults to the union of keys across all maps, in first-seen order.
Convert mapseq `ms` into rows with a header row. `:columns` (optional) gives explicit field order; defaults to the union of keys across all maps, in first-seen order.
(parse-rows
s
&
{:keys [separator] quote-char :quote :or {separator \, quote-char \"}})Parse CSV/TSV text s into a seq of rows, each row a vector of string fields.
Handles quoted fields (which may contain separators or newlines) and doubled quotes
as an escaped quote. Recognizes both \n and \r\n line endings.
Options:
:separator - field separator character (default ,)
:quote - quote character (default ")
Parse CSV/TSV text `s` into a seq of rows, each row a vector of string fields. Handles quoted fields (which may contain separators or newlines) and doubled quotes as an escaped quote. Recognizes both \n and \r\n line endings. Options: :separator - field separator character (default \,) :quote - quote character (default \")
(parse-tsv-rows s & opts)Like parse-rows, but defaults :separator to tab.
Like parse-rows, but defaults :separator to tab.
(read-csv s & opts)Parse CSV text s into raw rows. See parse-rows for options.
Parse CSV text `s` into raw rows. See parse-rows for options.
(read-csv-file source & opts)Read CSV rows from a file, resource, or url. See parse-rows for options.
Read CSV rows from a file, resource, or url. See parse-rows for options.
(read-csv-ms s & opts)Parse CSV text s into a mapseq, using the first row as the header.
Parse CSV text `s` into a mapseq, using the first row as the header.
(read-csv-ms-file source & opts)Read a CSV mapseq from a file, resource, or url. See parse-rows for options.
Read a CSV mapseq from a file, resource, or url. See parse-rows for options.
(read-tsv s & opts)Parse TSV text s into raw rows. See parse-rows for options.
Parse TSV text `s` into raw rows. See parse-rows for options.
(read-tsv-file source & opts)Read TSV rows from a file, resource, or url. See parse-rows for options.
Read TSV rows from a file, resource, or url. See parse-rows for options.
(read-tsv-ms s & opts)Parse TSV text s into a mapseq, using the first row as the header.
Parse TSV text `s` into a mapseq, using the first row as the header.
(read-tsv-ms-file source & opts)Read a TSV mapseq from a file, resource, or url. See parse-rows for options.
Read a TSV mapseq from a file, resource, or url. See parse-rows for options.
(rows->ms rows & {:keys [key-fn headers strings?] :or {key-fn header->key}})Convert rows into a mapseq: a seq of maps from header field to value.
Options:
:key-fn - header field -> map key (default header->key)
:headers - explicit header row, for data with no header row of its own;
default is to take the header from the first row of rows
:strings? - keep all values as strings; default false, which coerces an empty field
to nil and otherwise leaves numeric-looking strings as numbers
Convert `rows` into a mapseq: a seq of maps from header field to value.
Options:
:key-fn - header field -> map key (default header->key)
:headers - explicit header row, for data with no header row of its own;
default is to take the header from the first row of `rows`
:strings? - keep all values as strings; default false, which coerces an empty field
to nil and otherwise leaves numeric-looking strings as numbers(rows->str rows
&
{:keys [separator newline quote?]
quote-char :quote
:or {separator \, quote-char \" newline :lf}})Write rows (a seq of seqs of values, stringified with str) as CSV/TSV text.
Options:
:separator - field separator character (default ,)
:quote - quote character (default ")
:newline - line separator, :lf (default) or :cr+lf
:quote? - quote every field if true, or a predicate of the (stringified) field to
quote just that field; default nil, meaning auto-quote as needed
Write `rows` (a seq of seqs of values, stringified with str) as CSV/TSV text.
Options:
:separator - field separator character (default \,)
:quote - quote character (default \")
:newline - line separator, :lf (default) or :cr+lf
:quote? - quote every field if true, or a predicate of the (stringified) field to
quote just that field; default nil, meaning auto-quote as needed(rows->tsv-str rows & opts)Like rows->str, but defaults :separator to tab.
Like rows->str, but defaults :separator to tab.
(slurp-source source)Slurp text from anything clojure.java.io/reader accepts: a file, a filename, a classpath resource (io/resource), a URL (as a string or java.net.URL), etc. Strips a leading UTF-8 byte-order-mark if present (common in Excel exports).
Slurp text from anything clojure.java.io/reader accepts: a file, a filename, a classpath resource (io/resource), a URL (as a string or java.net.URL), etc. Strips a leading UTF-8 byte-order-mark if present (common in Excel exports).
(write-csv rows & opts)Write rows to CSV text. See rows->str for options.
Write `rows` to CSV text. See rows->str for options.
(write-csv-file file rows & opts)Write rows as CSV to file. See rows->str for options.
Write `rows` as CSV to `file`. See rows->str for options.
(write-csv-ms ms & opts)Write mapseq ms to CSV text, header row first. See ms->rows and rows->str for options.
Write mapseq `ms` to CSV text, header row first. See ms->rows and rows->str for options.
(write-csv-ms-file file ms & opts)Write mapseq ms as CSV to file, header row first. See ms->rows and rows->str for options.
Write mapseq `ms` as CSV to `file`, header row first. See ms->rows and rows->str for options.
(write-tsv rows & opts)Write rows to TSV text. See rows->str for options.
Write `rows` to TSV text. See rows->str for options.
(write-tsv-file file rows & opts)Write rows as TSV to file. See rows->str for options.
Write `rows` as TSV to `file`. See rows->str for options.
(write-tsv-ms ms & opts)Write mapseq ms to TSV text, header row first. See ms->rows and rows->str for options.
Write mapseq `ms` to TSV text, header row first. See ms->rows and rows->str for options.
(write-tsv-ms-file file ms & opts)Write mapseq ms as TSV to file, header row first. See ms->rows and rows->str for options.
Write mapseq `ms` as TSV to `file`, header row first. See ms->rows and rows->str for options.
cljdoc builds & hosts documentation for Clojure/Script libraries
| Ctrl+k | Jump to recent docs |
| ← | Move to previous article |
| → | Move to next article |
| Ctrl+/ | Jump to the search field |