jyriand
jyriand

Reputation: 2524

Clojure - Read large file line by line that is using different character for newline

I need to read in large csv file line by line. My only problem is that this file is not using \n for newline, but char 2. So what I would need is something line line-seq but with the ability to specify newline char.

Upvotes: 2

Views: 264

Answers (1)

Nicolas Modrzyk
Nicolas Modrzyk

Reputation: 14187

I am interested in the use case ;)

So I put up a quick library that hopefully does the job.

https://github.com/hellonico/custom-reader

Basically explained by the 2 test cases, so copy pasting them here:

With file test.txt

thisisaline 2thisisanotherline2thisisathirdline

This gives:

(deftest test-with-special-line-ending-char-has-3-lines
 (let [
   rdr (custom.java.BufferedReader. (java.io.FileReader. "resources/test.txt") \2)
  ]
  (is (= 3 (count (line-seq2 rdr))))))

And with a regular file:

this is
counted
as
only one line

This gives

   (deftest test-with-normal-line-endings    (let [
      rdr (custom.java.BufferedReader. 
            (java.io.FileReader. "resources/test2.txt") \2)
     ]
     (is (= 1 (count (line-seq2 rdr))))))

Let's see if this passes your large csv test.

Upvotes: 1

Related Questions