parsing - clojure csv with escaped comma -
i trying parse csv string in clojure escaped commas , no quote around fields one
"test1\,test2,test3"
i tried libraries:
- [org.clojure/data.csv "0.1.2"]
- [cljcsv "1.3.1"]
- [clojure-csv/clojure-csv "2.0.0-alpha1"]
but none of them seems capable recognize correctly ["test1,test2" "test3"]
know library can this?
thanks in advance
you have excellent excuse write own parser :-).
can use instaparse: https://github.com/engelberg/instaparse
update: ok, not resist temptation myself :-)
update 2: allow escaped characters inside quoted strings.
(require '[instaparse.core :as insta]) (def custom-csv (insta/parser "file = (line <eol>)* line line = (field <','>)* field eol = '\\r'? '\\n' <field> = plain-field | quoted-field quoted-field = <'\\\"'> (#'[^\"\\\\]+' | escaped-char)* <'\\\"'> plain-field = (field-chars | escaped-char)* <field-chars> = #'[^\\\\\\r\\n,\\\"]+' escaped-char = #'\\\\.' ")) (def test-str "test1\\,test2,test3 te\\s\\\\t4,\"te,st 5\"") (custom-csv test-str) ; result: ; [:file ; [:line ; [:plain-field "test1" [:escaped-char "\\,"] "test2"] ; [:plain-field "test3"]] ; [:line ; [:plain-field "te" [:escaped-char "\\s"] [:escaped-char "\\\\"] "t4"] ; "te,st\n5"]] (->> (custom-csv test-str) (insta/transform { :file list :line vector :plain-field str :quoted-field str :escaped-char second })) ; result: ; (["test1,test2" "test3"] ["tes\\t4" "te,st\n5"])