Created
August 13, 2012 18:33
-
-
Save bahmanm/3343020 to your computer and use it in GitHub Desktop.
Processing CSV contents
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
; 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