octopusgrabbus
octopusgrabbus

Reputation: 10695

How to add a non-quoted delimiter at the end of a Clojure sequence

I have lines of data in a sequence of sequences and each sequence is different but follows the general pattern as follows:

("44999" "186300" "194300" "0" "380600" "325" "57" "0")

When I write the sequence of sequences out to a file using

(defn write-csv-file
  "Writes a csv file using a key and an s-o-s"
  [out-sos out-file]

  (if (= dbg 1)
    (println (first out-sos), "\n", out-file))

  (spit out-file "" :append false)
  (with-open [out-data (io/writer out-file)]
      (csv/write-csv out-data out-sos)))
.
.
.
(write-csv-file out-re "re_values.csv")

the data comes out like this

44999,186300,194300,0,380600,325,57,0

That is exactly the way I want it (unquoted), except, I'd like a unquoted ',' at the end of each sequence.

I've tried (concat one-row (list \,)) and trying to add a ',' at the end of each sequence in a (list function, but I cannot get an unquoted ',' at the end of each sequence. How can I do this?

As a workaround, I can run files like this through sed to add the trailing comma, but I'd like to do it all in Clojure.

Upvotes: 0

Views: 146

Answers (3)

BillRobertson42
BillRobertson42

Reputation: 12883

Maybe concat an empty string to each sequence inside of out-sos. Concat is lazy, so shouldn't be expensive.

(with-open [out-data (io/writer out-file)]
  (csv/write-csv out-data (map #(concat % [""]) out-sos))))

Not sure what the csv library would do with an empty at the end though. Hopefully you would just get your empty element.

Upvotes: 1

Svante
Svante

Reputation: 51501

I think that you do not want to "add a comma" but add an empty field (which is then separated by a comma). So, you should simply add an empty string to your line sequences.

Upvotes: 2

user922621
user922621

Reputation: 390

Did you try :end-of-line setting to ",\n"

This is what the documentation says:

:end-of-line

A string containing the end-of-line character for writing CSV files.

Default value: \n

This what I tried:

(csv/write-csv data :end-of-line ",\n")

Upvotes: 0

Related Questions