davidsantiago.clojure-csv

https://github.com/davidsantiago/clojure-csv.git

git clone 'https://github.com/davidsantiago/clojure-csv.git'

(ql:quickload :davidsantiago.clojure-csv)
172

Clojure-CSV

Clojure-CSV is a small library for reading and writing CSV files. The main features:

This library aims to be as permissive as possible with respect to deviation from the standard, as long as the intention is clear. The only correctness checks made are those on the actual (minimal) CSV structure. For example, some people think it should be an error when lines in the CSV have a different number of fields – you should check this yourself. However, it is not possible, after parsing, to tell if the input ended before the closing quote of a field; if you care, it can be signaled to you.

The API has changed in the 2.0 series; see below for details.

Recent Updates

Previously…

Obtaining

If you are using Leiningen, you can simply add

[clojure-csv/clojure-csv "2.0.1"]

to your project.clj and download it from Clojars with

lein deps

Use

The clojure-csv.core namespace exposes two functions to the user:

parse-csv

Takes a CSV as a char sequence or string, and returns a lazy sequence of vectors of strings; each vector corresponds to a row, and each string is one field from that row. Be careful to ensure that if you read lazily from a file or some other resource that it remains open when the sequence is consumed.

Takes the following keyword arguments to change parsing behavior:

:delimiter

A character that contains the cell separator for each column in a row.

Default value: \,

:end-of-line

A string containing the end-of-line character for reading CSV files. If this setting is nil then \n and \r\n are both accepted.

Default value: nil

:quote-char

A character that is used to begin and end a quoted cell.

Default value: "

:strict

If this variable is true, the parser will throw an exception on parse errors that are recoverable but not to spec or otherwise nonsensical.

Default value: false

write-csv

Takes a sequence of sequences of strings, basically a table of strings, and renders that table into a string in CSV format. You can easily call this function repeatedly row-by-row and concatenate the results yourself.

Takes the following keyword arguments to change the written file:

:delimiter

A character that contains the cell separator for each column in a row.

Default value: \,

:end-of-line

A string containing the end-of-line character for writing CSV files.

Default value: \n

:quote-char

A character that is used to begin and end a quoted cell.

Default value: "

:force-quote

If this variable is true, the output will have ever field quoted, whether this is needed or not. This can apparently be helpful for interoperating with Excel.

Default value: false

Changes from API 1.0

Clojure-CSV was originally written for Clojure 1.0, before many of the modern features we now enjoy in Clojure, like keyword args, an IO library and fast primitive math. The 2.0 series freshens up the API to more modern Clojure API style, language capabilities, and coding conventions. The JARs for the 1.0 series will remain available indefinitely (probably a long, long time), so if you can't handle an API change, you can continue to use it as you always have.

Here's a summary of the changes:

Bugs

Please let me know of any problems you are having.

Contributors

License

Eclipse Public License