Quantcast
Channel: Community Center - ClojureVerse
Viewing all articles
Browse latest Browse all 168

Basic CSV reading with Numbers

$
0
0

I’m trying to read a single small delimited list of numbers from a CSV file. When using slurp, if I’m reading the problem below correctly, the data is being imported as Java.String so I cannot use +,- apply, map in anyway in the collection. Also I find it odd the collection remains in quotation marks [“1,2,3,4”] and not [1,2,3,4]. My best guess was to find a typecasting function and I thought cast or to-array would work but I run into same error message.

For a solution, the Clojure data structure / type the data is held is unimportant to me as long as I can use apply, reduce.

I’ve looked online, in docs to find a solution and would be grateful to any help in this matter.

I considered external libraries like for CSV, changing data format but seem to run into the same problem. I also considered using SQLite but think this is just adding complexity I don’t want /need.

In lein repl, Clj 1.11.1 JDK 8 (another system with JDK 11 same issue) :

=> (def x (-> (slurp “nums.csv” ) (clojure.string/split #“\n”)))
[“1,2,3,4”]
=> (type x)
clojure.lang.PersistentVector
=> (apply + x)
Execution error (ClassCastException) at java.lang.Class/cast (Class.java:3369).
Cannot cast java.lang.String to java.lang.Number

3 posts - 3 participants

Read full topic


Viewing all articles
Browse latest Browse all 168

Trending Articles