On Thu, Jun 16, 2011 at 6:07 PM, octopusgrabbus
<octopusgrab...@gmail.com> wrote:
> This Clojure program:
>
> ns test-csv
>  (:require [clojure.contrib.string :as str])
>  (:import (java.io BufferedReader FileReader StringReader))
>  (:use clojure-csv.core))
>
>
> (defn process-file [file-name]
>    (with-open [br (BufferedReader. (FileReader. file-name))]
>                             (println (line-seq br))))
>
>
> (defn -main [& args]
>  (process-file "resultset.csv"))
>
> is printing a test file. I believe line-seq returns a sequence of
> strings. I am having trouble figuring out where to call str/split. The
> error I'm getting back is you can cast to a regular expression. Any
> thoughts?

The split function takes a regular expression for its split-with-this
argument, rather than a regular string. In many cases simply changing
a string literal to a regex literal will do, e.g. with CSV splitting
"," to #",".

If you (map #(split...) (line-seq foo)) on your CSV data you'll end up
with a seq of seqs of strings, each outer seq a row and each inner seq
the cells of that row. If there are escaped embedded commas in some of
the cell value strings they'll still be escaped, however it is that
they are escaped, though, so you might want to put the whole thing
through (map #(map unescape %) outer-seq) to unescape everything after
the splitting; implementing the unescape function is left as an
exercise for the reader. :)

Note that all of this stuff is lazy, so you'll want to keep the reader
open until you've done processing everything and have a result to
return, and if the result is a seq it may need doall called on it
first depending on how it is constructed. On the other hand this means
you can fairly transparently work with CSV files larger than main
memory.
-- 
Some people, when confronted with a problem, think “I know, I'll use
regular expressions.”
Now they have two problems.
- Jamie Zawinski

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to