1. Miki Tebeka
  2. clojurewise

Commits

Miki Tebeka  committed 4f30eb9

CSV files

  • Participants
  • Parent commits 4b3604e
  • Branches default

Comments (0)

Files changed (1)

File csv.clj

View file
+(ns csv
+  (import com.csvreader.CsvReader))
+
+(defn- record-seq [filename]
+  (let [csv (CsvReader. filename)
+        read-record (fn [] 
+                      (when (.readRecord csv) 
+                        (into [] (.getValues csv))))]
+    (take-while (complement nil?) (repeatedly read-record))))
+
+(defn csv-seq
+  "Return a lazy sequence of records (maps) from CSV file.
+
+  With header map will be header->value, otherwise it'll be position->value."
+  ([filename] (csv-seq filename false))
+  ([filename headers?]
+   (let [records (record-seq filename)
+         headers (if headers? (first records) (range (count records)))]
+    (map #(zipmap headers %) (if headers? (rest records) records)))))
+