Created
February 18, 2009 17:55
-
-
Save drewr/66446 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(ns com.notifymd.zip | |
(:import [com.csvreader CsvReader])) | |
(defn slurp-csv | |
"Return a map where key equals (keyword \"37211\") and | |
value equals the rest of the row minus the zip code" | |
[filename] | |
(let [reader (doto (CsvReader. filename) | |
(.readHeaders)) | |
headers (map #(keyword (.toLowerCase %)) | |
(seq (.getHeaders reader))) | |
row-struct (apply create-struct (filter #(not (= :zip %)) headers)) | |
row-seq #(interleave %1 %2) | |
row-ds (fn [row] | |
(let [m (apply struct-map row-struct row)] | |
{(keyword (:zip m)) (dissoc m :zip)})) | |
rows (fn rows [hs rdr] | |
(when (.readRecord rdr) | |
(let [row (row-ds (row-seq hs (seq (.getValues rdr))))] | |
(lazy-cons row (rows hs rdr))))) | |
m (reduce conj {} (rows headers reader))] | |
(.close reader) | |
m)) | |
(comment | |
(def zips1 (slurp-csv "/Users/aar/Downloads/OE_ZIP.csv")) | |
(def zips2 (slurp-csv "/Users/aar/Downloads/OE_ZIP.csv")) | |
(count zips1) ; 65535 | |
(map :city (vals (select-keys zips1 [:37212 :37067]))) ; ("FRANKLIN" "NASHVILLE") | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment