Created
April 6, 2016 08:20
-
-
Save samuelsmal/f51de1250940dbc53dfced36b9bb3c94 to your computer and use it in GitHub Desktop.
Clojure script to extract some fields from a edn-file and store it as a CSV file
This file contains hidden or 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
#!/usr/bin/env boot | |
(require '[clojure.java.io :as io]) | |
(require '[clojure.edn :as edn]) | |
(def fname "/path/to/clojure-edn.clj") | |
(def ofname "/path/to/out.csv") | |
;; out-format: | |
;; "keykeykey", "valvalval" | |
(defn mkcsv | |
[m] | |
(str | |
"\"" | |
(:key m) | |
"\",\"" | |
(get-in m [:value :another-key]) | |
"\"\n")) | |
(defn -main [& args] | |
(let [in (-> fname io/file io/reader) | |
out (-> ofname io/file io/writer)] | |
(try | |
(loop [i 0] | |
(when-some [line (.readLine in)] | |
(let [edn (edn/read-string line)] | |
;; write | |
(.write out (mkcsv edn))) | |
(when (zero? (mod i 100)) | |
(.flush out)) | |
(recur (inc i)))) | |
(catch Exception ex | |
(println "Exception occured: " ex)) | |
(finally | |
(.close in) | |
(.close out))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment