Skip to content

Instantly share code, notes, and snippets.

@bahmanm
Created August 13, 2012 18:33
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 bahmanm/3343020 to your computer and use it in GitHub Desktop.
Save bahmanm/3343020 to your computer and use it in GitHub Desktop.
Processing CSV contents
; Suppose you have a CSV file with the first row being the column names
; and the rest being data. Using the following snippet you can process
; the contents passing each row's contents as keyword arguments to
; another function.
;
; Example CSV structure:
; +----------------------------------------------------------+
; | customer-code | name | e-mail | address | phone |
; +----------------------------------------------------------+
;
; CSV parsing is done using (https://github.com/davidsantiago/clojure-csv)
(defn processing-fn [{:keys [customer-code name e-mail address phone]}]
; do useful stuff
)
(defn read-csv [file]
(let [csv (csv/parse-csv (slurp (io/file (:tempfile file))))]
(map processing-fn
; create a vector of maps each of which containing column names
; as keys and column data as value
(map (fn [ks vs]
(into {}
(map (fn [k v] {(keyword k) v})
ks vs)))
(repeat (first csv)) (rest csv)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment