How to read a file with test data in with Clojure?

Viewed 2483

I am writing a piece of code that needs to read in a text file that has data. The text file is in the format:

name 1 4
name 2 4 5
name 3 1 9

I am trying to create a vector of a map in the form [:name Sarah :weight 1 cost :4].

When I try reading the file in with the line-seq reader, it reads each line as an item so the partition is not correct. See repl below:

(let [file-text (line-seq (reader "C://Drugs/myproject/src/myproject/data.txt"))
                       new-test-items (vec (map #(apply struct item %) (partition 3 file-text)))]
     (println file-text)   
     (println new-test-items))


(sarah 1 1 jason 4 5 nila 3 2  jonas 5 6 judy 8 15 denny 9 14 lis 2 2  )
[{:name sarah 1 1, :weight jason 4 5, :value nila 3 2 } {:name jonas 5 6, :weight judy 8 15, :value denny 9 14}]

I then tried to just take 1 partition, but still the structure is not right.

=> (let [file-text (line-seq (reader "C://Drugs/myproject/src/myproject/data.txt"))
                                new-test-items (vec (map #(apply struct item %) (partition 1 file-text)))]
              (println file-text)   
              (println new-test-items))
(sarah 1 1 jason 4 5 nila 3 2  jonas 5 6 judy 8 15 denny 9 14 lis 2 2  )
[{:name sarah 1 1, :weight nil, :value nil} {:name jason 4 5, :weight nil, :value nil} {:name nila 3 2 , :weight nil, :value nil} {:name jonas 5 6, :weight nil, :value nil} {:name judy 8 15, :weight nil, :value nil} {:name denny 9 14, :weight nil, :value nil} {:name lis 2 2, :weight nil, :value nil} {:name  , :weight nil, :value nil}]
nil

Next I tried to slurp the file, but that is worse:

=> (let [slurp-input (slurp "C://Drugs/myproject/src/myproject/data.txt")
            part-items (partition 3 slurp-input)
            mapping (vec (map #(apply struct item %) part-items))]
            (println slurp-input)
            (println part-items)
            (println mapping))
sarah 1 1
jason 4 5
nila 3 2 
jonas 5 6
judy 8 15
denny 9 14
lis 2 2

((s a r) (a h  ) (1   1) (

Please help! This seems like such an easy thing to do in Java, but is killing me in Clojure.

2 Answers
Related