由于记录是不可变的,我无法读取数据并解析它而不会创建自己的新实例。此外,我如何能够从多个特定列读取我的Excel文件,而不是从第0列读取到EOF。无论如何,我可以从第1列第3列第5列读取数据。据说,第1列将被解析为字符串,第3列将被解析为整数,第5列将被解析为长。
(defrecord Record [Name Age Index])
(defn read-csv [fname count]
(with-open [file (io/reader fname)]
(doall (take count (map (comp first csv/read-csv)
(line-seq file))))))
(def records (map #(apply ->Record %) (read-csv "C:/Users/user/Documents/URECA/hi/lib/test.csv" 1)))
这就是我所拥有的,但它似乎以递增方式读取列
答案 0 :(得分:1)
要保留文本字段的引号,可以通过regexp解析csv文件:
(defn read-csv [fname count]
(with-open [file (io/reader fname)]
(doall (map #(str/split % #",") ; doesn't work with commas in text fields
(take count (line-seq file))))))
(defn make-record [idxs types row]
(apply ->Record
(map (fn [idx t]
(let [value (nth row idx)]
(case t
:string value
:int (Integer/parseInt value)
:long (Long/parseLong value))))
idxs types)))
(def records (map (partial make-record
[0 2 4]
[:string :int :long])
(read-csv "/home/mobyte/test.csv" 3)))
(pprint records)
-> ({:Name "\"s1\"", :Age 1, :Index 111}
{:Name "\"s2\"", :Age 2, :Index 112}
{:Name "\"s3\"", :Age 3, :Index 113})
(type (:Age (first records)))
->java.lang.Integer
(type (:Index (first records)))
-> java.lang.Long
(type (:Name (first records)))
-> java.lang.String