> I'm doing the same. I cannot judge the quality of OpenCVS but up to
> now I had no problems. I thought about using fnparse to build a
> clojure
> CSV parser, but I'm not sure how hard this would be. Let's see with
> what Stuart comes up.
CSV parsing is a headache because there are so many improperly
formatted files that "just work" with different libraries. I figured
due to OpenCSV's age it probably was pretty good at it and left it at
that.
> I built a function which reads the first row in the file as headers
> and
> creates a sequence of structures of the rest.
>
> (defn entry-seq
> [rdr]
> (let [hdrs (map keyword (.readNext rdr))
> entry-s (apply create-struct hdrs)
> entry-seq (fn entry-seq []
> (lazy-seq
> (when-let [entry (.readNext rdr)]
> (cons (apply struct entry-s entry) (entry-
> seq)))))]
> (entry-seq)))
This is great! I did rework it to use the other function I already had
(read-csv) but otherwise stole your whole approach and came up with
this:
(defn remove-comment
[[first & rest]]
"Removes a leading # from the first element in the vector."
(vec (cons (if (= (.charAt first 0) \#) (.substring first 1) first)
rest)))
(defn parse-csv
"Parses a CSV file and returns a seq of dictionaries using the
first row as
headers."
[filename]
(let [csv-data (read-csv filename)
entry-struct (apply create-struct
(map keyword (remove-comment (first csv-data))))]
(map #(apply struct entry-struct %) (rest csv-data))))
—
Daniel Lyons
http://www.storytotell.org -- Tell It!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---