Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

read tab delimited file in clojure

How do I read a tab-delimited file using Clojure? There may be whitespaces in a line which do not correspond to a tab.

E.g.: transform

some field     another-field     a third field

into

["some field" "another-field" "a third field"]
like image 535
animalcroc Avatar asked Oct 16 '25 12:10

animalcroc


1 Answers

You can use the data.csv Contrib library:

;; in your :dependencies
[org.clojure/data.csv "0.1.2"]

;; at the REPL
(require '[clojure.data.csv :as csv])

(csv/read-csv
  (java.io.StringReader. "some field\tanother-field\ta third field")
  :separator \tab)
;= (["some field" "another-field" "a third field"])

(Use something like (with-open [rdr (clojure.java.io/reader f)] (vec (csv/read-csv rdr :separator \tab))) to read data from the TSV file f.)

like image 71
Michał Marczyk Avatar answered Oct 18 '25 05:10

Michał Marczyk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!