Skip to content

Instantly share code, notes, and snippets.

@DaveWM
Last active July 10, 2020 14:24
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 DaveWM/3185481497d32ca623838137e77bd291 to your computer and use it in GitHub Desktop.
Save DaveWM/3185481497d32ca623838137e77bd291 to your computer and use it in GitHub Desktop.
#!/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