Liking cljdoc? Tell your friends :D

hyperphor.multitool.csv

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.

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.
raw docstring

header->keyclj/s

(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.
sourceraw docstring

ms->rowsclj/s

(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.
sourceraw docstring

parse-rowsclj/s

(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 \")
sourceraw docstring

parse-tsv-rowsclj/s

(parse-tsv-rows s & opts)

Like parse-rows, but defaults :separator to tab.

Like parse-rows, but defaults :separator to tab.
sourceraw docstring

read-csvclj/s

(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.
sourceraw docstring

read-csv-fileclj

(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.
sourceraw docstring

read-csv-msclj/s

(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.
sourceraw docstring

read-csv-ms-fileclj

(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.
sourceraw docstring

read-tsvclj/s

(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.
sourceraw docstring

read-tsv-fileclj

(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.
sourceraw docstring

read-tsv-msclj/s

(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.
sourceraw docstring

read-tsv-ms-fileclj

(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.
sourceraw docstring

rows->msclj/s

(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
sourceraw docstring

rows->strclj/s

(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
sourceraw docstring

rows->tsv-strclj/s

(rows->tsv-str rows & opts)

Like rows->str, but defaults :separator to tab.

Like rows->str, but defaults :separator to tab.
sourceraw docstring

slurp-sourceclj

(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).
sourceraw docstring

write-csvclj/s

(write-csv rows & opts)

Write rows to CSV text. See rows->str for options.

Write `rows` to CSV text. See rows->str for options.
sourceraw docstring

write-csv-fileclj

(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.
sourceraw docstring

write-csv-msclj/s

(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.
sourceraw docstring

write-csv-ms-fileclj

(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.
sourceraw docstring

write-tsvclj/s

(write-tsv rows & opts)

Write rows to TSV text. See rows->str for options.

Write `rows` to TSV text. See rows->str for options.
sourceraw docstring

write-tsv-fileclj

(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.
sourceraw docstring

write-tsv-msclj/s

(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.
sourceraw docstring

write-tsv-ms-fileclj

(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.
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close