CJLam
CJLam

Reputation: 851

Clojure CSV Parsing Commas in Quotes

I have a CSV file as follow (EDITED: note the space after the first comma)

0, "a,a,a,a"
1, "b,b,b,b"
2, "c,c,c,c"

and I am hoping to parse each row as a vector of 2 strings, i.e.

[0 "a,a,a,a"]
[1 "b,b,b,b"]
[2 "c,c,c,c"]

in such way that the commas within quotes are preserved.

However, both data.csv and clojure-csv provide vectors of 5 strings:

[0 "a a a a"]
[1 "b b b b"]
[2 "c c c c"]

I've tried different values of parser options (:quote-char ") but had still no luck.

Here's my code:

(with-open [in-file (io/reader "myData.csv")]
  (doall 
    (map println
     (take 10 (parse-csv in-file)))))

Upvotes: 1

Views: 524

Answers (1)

piyushmandovra
piyushmandovra

Reputation: 4381

There is clojure.data.csv:

(:require [clojure.data.csv :as csv])
(csv/read-csv (slurp "myData.csv"))

It will generate output:

(["0" "a,a,a,a"] ["1" "b,b,b,b"] ["2" "c,c,c,c"])

Upvotes: 1

Related Questions