clojure-csv Extracting one column from each line

469 views
Skip to first unread message

octopusgrabbus

unread,
Jun 17, 2011, 10:06:50 AM6/17/11
to Clojure
This question is more about knowing what is returned after applying a
function and the best "functional programming" practices to use in
obtaining particular data in what has been returned.

So, given this program:

(ns test-csv
(:gen-class)
(:import (java.io BufferedReader FileReader StringReader))
(:use clojure-csv.core))

(defn process-file [file-name]
(with-open [br (BufferedReader. (FileReader. file-name))]
(println (parse-csv (line-seq br)))))

(defn -main [& args]
(process-file "resultset.csv"))

and this data:

"MeterID","Reading","ReadingDateTime","Account","CustomerLN","CustomerFN","DeviceID","DeviceType","ChannelNumber","DecodeType","LoadDateLocal","PremiseID"
33891715,101100,"2011-06-05 23:00:00","610160000","SMITH","E & J",
80581200,43,0,75,"2011-06-06 06:00:01","610160000"
33891773,411200,"2011-06-05 23:00:00","610159000","COMMONER","A",
80598726,43,0,75,"2011-06-06 06:00:01","610159000"
33891887,133100,"2011-06-05 23:00:00","610158000","JONES","J & M",
80581189,43,0,75,"2011-06-06 06:00:01","610158000"
33891825,239400,"2011-06-05 23:00:00","610157000","SAWTOOTH","GEORGE
C",80598731,43,0,75,"2011-06-06 06:00:01","610157000"

and running this:

cnorton@steamboy:~/projects/clojure/test_csv$ ./test-csv

This printed out:

(["MeterID","Reading","ReadingDateTime","Account","CustomerLN","CustomerFN","DeviceID","DeviceType","ChannelNumber","DecodeType","LoadDateLocal","PremiseID"33891715,101100,"2011-06-05
23:00:00","610160000","SMITH","E & J",80581200,43,0,75,"2011-06-06
06:00:01","610160000"33891773,411200,"2011-06-05
23:00:00","610159000","COMMONER","A",80598726,43,0,75,"2011-06-06
06:00:01","610159000"33891887,133100,"2011-06-05
23:00:00","610158000","JONES","J & M",80581189,43,0,75,"2011-06-06
06:00:01","610158000"33891825,239400,"2011-06-05
23:00:00","610157000","SAWTOOTH","GEORGE C",
80598731,43,0,75,"2011-06-06 06:00:01","610157000"])
cn

I have tried various ways using nth and index 0, as well as re-writing
this into a plain file/io example and using clojure.string/split at
comma boundaries, but cannot get a particular column out of each line
in the original file. All efforts so far obtain the first line in the
file and nothing else. I'm trying to figure out how to get the first
column of each line, but it might as well be the nth column of any
line.

The plain I/O method made more sense to me, because after calling map,
because it seemd a sequence of string vectors had been returned, at
least that's how it looked after printing out. The result of printing
the clojure-csv example appears to be one vector. So, I am wondering
how to use clojure-csv to parse out one or more columns in a given
line.

Thanks again for the help.
cmn

Miki

unread,
Jun 17, 2011, 10:46:55 AM6/17/11
to clo...@googlegroups.com
parse-csv returns a sequence of vectors. The "functional way" of traversing a sequence is using map:

(ns foo
  (:gen-class)

  (:use clojure-csv.core))

(defn process-file
  "Process csv file and prints first item in every row"
  [file-name]
  (let [data (slurp file-name)
        rows (parse-csv data)]
    (dorun (map #(println (first %)) rows))))

octopusgrabbus

unread,
Jun 17, 2011, 11:00:05 AM6/17/11
to Clojure
Thanks for your answer. I remember the notes on clojure-csv saying it
employed lazy I/O. So, it looks like by using slurp, there is still
lazy-io going on.

Miki

unread,
Jun 17, 2011, 5:36:19 PM6/17/11
to clo...@googlegroups.com


So, it looks like by using slurp, there is still
lazy-io going on.
I don't think slurp is lazy.

user=> (doc slurp)
-------------------------
clojure.core/slurp
([f & opts])
  Reads the file named by f using the encoding enc into a string
  and returns it.
nil
user=>


Reply all
Reply to author
Forward
0 new messages