Last active
July 10, 2020 14:24
-
-
Save DaveWM/3185481497d32ca623838137e77bd291 to your computer and use it in GitHub Desktop.
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
#!/usr/bin/env bb | |
;; ^^ this tells our shell to use Babashka to run this script | |
;; read the file path of our CSV and the key field from the command line args | |
(def csv-file-path (first *command-line-args*)) | |
(def key-field (second *command-line-args*)) | |
;; read the CSV line-by-line into a data structure | |
(def csv-data | |
(with-open [reader (io/reader csv-file-path)] | |
(doall (csv/read-csv reader)))) | |
(def headers (first csv-data)) | |
(def body (rest csv-data)) | |
;; For each line in the body, create a map with the headers as the keys | |
(def values | |
(->> body | |
(map (partial zipmap headers)) | |
;; if you need to do any additional processing on each line, do it here | |
)) | |
(def output-lines | |
(->> values | |
(map #(str (get % key-field) "::" (json/generate-string %))))) | |
(doseq [output output-lines] | |
(println output)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment