Instantly share code, notes, and snippets.

What would you like to do?
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 (
(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