Skip to content

Instantly share code, notes, and snippets.

@FiV0
Created March 22, 2023 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FiV0/94dfa1573626921236b6a929e6e6397c to your computer and use it in GitHub Desktop.
Save FiV0/94dfa1573626921236b6a929e6e6397c to your computer and use it in GitHub Desktop.
Too often written from scratch... (CSV reading/writing)
(require '[clojure.java.io :as io]
'[clojure.data.csv :as csv]
'[clojure.string :as str])
(defn header [edn-map]
(->> edn-map keys (map name)))
(defn write-to-csv-file
([file edn-results] (write-to-csv-file file edn-results {}))
([file edn-results {:keys [append write-header] :or {write-header true} :as _opts}]
(with-open [writer (io/writer file :append append)]
(csv/write-csv writer
(cond->> (map vals edn-results)
write-header (cons (header (first edn-results))))))))
(defn csv->maps
[[csv-headers & csv-data]]
(map zipmap (repeat (map keyword csv-headers)) csv-data))
(defn normalize-header
[[hd & tl]]
(->> (mapv #(str/replace % #"_" "-") hd)
(conj tl)))
(defn resolve-suitable-separators [file]
(let [first (-> file slurp str/split-lines first)
comma (count (re-seq #"," first))
dcoma (count (re-seq #";" first))]
(if (> comma dcoma) \, \;)))
(defn read-csv [file]
(let [csv-separator (resolve-suitable-separators file)]
(with-open [rdr (io/reader file)]
(->> (csv/read-csv rdr :separator csv-separator)
normalize-header
csv->maps
doall))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment