Created
March 22, 2023 13:48
-
-
Save FiV0/94dfa1573626921236b6a929e6e6397c to your computer and use it in GitHub Desktop.
Too often written from scratch... (CSV reading/writing)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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