octopusgrabbus
octopusgrabbus

Reputation: 10695

How to strip double quote (") characters from sequence

I have a sequence of sequences and each sequence is similar to the following:

("9990999" "43" "ROADWAY" "MORRISON, VAN X DMD" "43 ROADWAY" "SOMETHINGTON" "XA" "00000" "501" "18050" "2500" "1180" "14370" "0")

clojure-csv won't help me here, because it -- as it should -- quotes fields with embedded commas. I want pipe-delimited output without quotes around each field, some of which contain embedded commas.

I have looked at a number of ways to remove the double quote characters including the following, but the quotes stay put.

(filter (fn [x] (not (= (str (first (str x))) (str (first (str \")))))) d1)

where d1 is the sequence above.

In addition to an answer, I am more interested in a pointer to documentation. I have been playing with this but to no avail.

Upvotes: 1

Views: 2535

Answers (1)

Ivan Koblik
Ivan Koblik

Reputation: 4315

As far as I understand you have a sequence of strings. Clojure provides a very specific toString implementation for sequences, you can see it here.

If you do (str d1) or simply type d1 in repl and press enter you'll see more or less what you typed: sequence of strings (String is printed as sequence of characters in double quotes).

Now if you want to concatenate all the string you can do this:

(apply str d1)

If you want to print it separated with commas you could do this:

(apply str (interpose "," d1))

To output is CSV format I would recommend to use clojure-csv.

Finally if you simply want to print the list but without the double quotes around strings you could do this:

(print d1)

Hope this helps.


EDIT1 (update due to changes in the question):

This can easily be achieved with:

(apply str (interpose "|" d1))

Please don't pay attention to double quotes around the entire result if you print it or spit it into a file you won't see them, this is just how Clojure prints string readably.

Alternatively if you have multiple sequences like that that you want to output at once you can still use clojure-csv but with different separator:

(ns csv-test.core
  (:require [clojure-csv.core :as csv]))

(def d1 (list "9990999" "43" "ROADWAY" "MORRISON, VAN X DMD" "43 ROADWAY" "SOMETHINGTON" "XA" "00000" "501" "18050" "2500" "1180" "14370" "0"))
(print (csv/write-csv [d1]  :delimiter "|"))
;;prints:
;;9990999|43|ROADWAY|MORRISON, VAN X DMD|43 ROADWAY|SOMETHINGTON|XA|00000|501|18050|2500|1180|14370|0

Upvotes: 5

Related Questions