On Tue, Nov 19, 2013 at 6:24 AM, smk <mshanm...@hotmail.com> wrote: > Hi > I am trying to parse a text file that has some binary characters(such as > 0x001 or 0x002) and replace them with text characters. > How to do it in clojure? >
Would something like this work? (require '[clojure.java.io :as io]) (def replace-chars "A map of characters to their replacements." (let [m {0x01 \X, 0x02 \Y, 0x03 \Z}] (into {} (map (fn [[k v]] [(char k) v]) m)))) (defn read-lines [filename] (with-open [rdr (io/reader filename)] (doall (line-seq rdr)))) (defn read-and-replace [filename] (for [line (read-lines filename)] (apply str (map #(get replace-chars % %) line)))) I tried it like this: (def test-file "/home/jbm/text-and-binary.txt") (defn create-test-file [filename] (with-open [wtr (io/writer filename)] (.write wtr "foo") (.write wtr 0x01) (.write wtr "bar") (.write wtr 0x02) (.write wtr "baz"))) (create-test-file test-file) (read-lines test-file) ;=> ("foo^Abar^Bbaz") (read-and-replace test-file) ;=> ("fooXbarYbaz") - John -- -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.