Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Clojure 将哈希映射集合转换为csv文件_Clojure - Fatal编程技术网

Clojure 将哈希映射集合转换为csv文件

Clojure 将哈希映射集合转换为csv文件,clojure,Clojure,我有以下哈希映射集合: {a:"Completed" b:1 c:"Friday" d:4} {a:"Started" b:1 c:"Monday" d:4} {a:"In Progress" b:1 c:"Sunday" d:1} {a:"Completed" b:3 c:"Tuesday" d:9} 如何在clojure中将其转换为CSV文件 i、 e 在此方面的任何帮助都将不胜感激 用于将json转换为clojure映射序列 使用map destructuring转换为字符串序列:(ma

我有以下哈希映射集合:

{a:"Completed" b:1 c:"Friday" d:4}
{a:"Started" b:1 c:"Monday" d:4}
{a:"In Progress" b:1 c:"Sunday" d:1}
{a:"Completed" b:3 c:"Tuesday" d:9}
如何在clojure中将其转换为CSV文件

i、 e

在此方面的任何帮助都将不胜感激

  • 用于将json转换为clojure映射序列

  • 使用map destructuring转换为字符串序列:
    (map#(let[{a:ab:bc:cdd:d}%](stra”,“b”,“c”,“d,“\n”))

  • 将字符串序列转储到文件中


  • 您没有明确地说您是从JSON开始的,但我们假设您是。 我会用 解析JSON字符串以获取(在本例中)映射向量 表示您的数据。我们认为你这样做是为了 为了简单起见,只需使用名为
    data
    var
    来避免 混乱的例子

    现在我们可以创建一个向量向量,然后使用 拯救世界 将结果保存到文件中

    您可以使用在REPL上尝试这一点。 按照
    lein try
    的设置说明进行操作,如果您还没有,请运行
    lein试试clojure.data.csv 0.1.2
    。一旦您处于具有此依赖项的REPL中:

    (require '[clojure.data.csv :as csv] '[clojure.java.io :as io])
    
    (def data
      [{:a "Completed"   :b 1 :c "Friday"  :d 4}
       {:a "Started"     :b 1 :c "Monday"  :d 4}
       {:a "In Progress" :b 1 :c "Sunday"  :d 1}
       {:a "Completed"   :b 3 :c "Tuesday" :d 9}])
    
    (defn write-csv [path row-data]
      (let [columns [:a :b :c :d]
            headers (map name columns)
            rows (mapv #(mapv % columns) row-data)]
        (with-open [file (io/writer path)]
          (csv/write-csv file (cons headers rows)))))
    
    (write-csv "/tmp/results.csv" data)
    
    现在,您可以看到您的工作成果:

    $ cat /tmp/results.csv
    a,b,c,d
    Completed,1,Friday,4
    Started,1,Monday,4
    In Progress,1,Sunday,1
    Completed,3,Tuesday,9
    
    要查看结果,请执行以下操作:

    $ cat data.csv
    c,b,d,a
    Friday,1,4,Completed
    Monday,1,4,Started
    Sunday,1,1,In Progress
    Tuesday,3,9,Completed
    

    这个答案更好,因为这个方法从数据中获取所有内容,并且您不必硬编码任何内容。而且这种方法除了使用clojure.string/join之外,不使用任何额外的库。使用该库,可以轻松地读写格式良好的csv。

    我阅读了所有答案,但我想要更通用和可重用的解决方案

    这是我想到的。您可以单独使用
    write csv
    maps->csv数据
    ,也可以通过
    write csv from maps
    一起使用。更重要的是,您不需要硬编码头。只需传递你的地图向量,你就应该准备好了

    A.DIY方法

    (require '[clojure.data.csv :as csv]
             '[clojure.java.io :as io])
    
    (defn write-csv 
      "Takes a file (path, name and extension) and 
       csv-data (vector of vectors with all values) and
       writes csv file."
      [file csv-data] 
      (with-open [writer (io/writer file)]
        (csv/write-csv writer csv-data)))
    
     (defn maps->csv-data 
      "Takes a collection of maps and returns csv-data 
       (vector of vectors with all values)."       
       [maps]
       (let [columns (-> maps first keys)
             headers (mapv name columns)
             rows (mapv #(mapv % columns) maps)]
         (into [headers] rows)))
    
    (defn write-csv-from-maps 
      "Takes a file (path, name and extension) and a collection of maps
       transforms data (vector of vectors with all values) 
       writes csv file."
      [file maps]
      (->> maps maps->csv-data (write-csv file)))
    
    B.语义CSV方法

    或者,您可以使用库。
    maps->csv数据
    将变成
    semantic csv.core/vectorize

    (require '[clojure.data.csv :as csv]
             '[clojure.java.io :as io]
             '[semantic-csv.core :as sc])
    
    (defn write-csv 
      "Takes a file (path, name and extension) and 
       csv-data (vector of vectors with all values) and
       writes csv file."
      [file csv-data] 
      (with-open [writer (io/writer file)]
        (csv/write-csv writer csv-data)))
    
    (defn write-csv-from-maps 
      "Takes a file (path, name and extension) and a collection of maps
       transforms data (vector of vectors with all values) 
       writes csv file."
      [file maps]
      (->> maps sc/vectorize (write-csv file)))
    
    --

    最后:

    (def data
      [{:a "Completed"   :b 1 :c "Friday"  :d 4}
       {:a "Started"     :b 1 :c "Monday"  :d 4}
       {:a "In Progress" :b 1 :c "Sunday"  :d 1}
       {:a "Completed"   :b 3 :c "Tuesday" :d 9}])
    
    (write-csv "/tmp/results.csv" data)
    

    希望对别人有用

    请看一句话,您的hashmap不是用edn(Clojure)表示法,而是用JSON表示的。看见
    (require '[clojure.data.csv :as csv]
             '[clojure.java.io :as io]
             '[semantic-csv.core :as sc])
    
    (defn write-csv 
      "Takes a file (path, name and extension) and 
       csv-data (vector of vectors with all values) and
       writes csv file."
      [file csv-data] 
      (with-open [writer (io/writer file)]
        (csv/write-csv writer csv-data)))
    
    (defn write-csv-from-maps 
      "Takes a file (path, name and extension) and a collection of maps
       transforms data (vector of vectors with all values) 
       writes csv file."
      [file maps]
      (->> maps sc/vectorize (write-csv file)))
    
    (def data
      [{:a "Completed"   :b 1 :c "Friday"  :d 4}
       {:a "Started"     :b 1 :c "Monday"  :d 4}
       {:a "In Progress" :b 1 :c "Sunday"  :d 1}
       {:a "Completed"   :b 3 :c "Tuesday" :d 9}])
    
    (write-csv "/tmp/results.csv" data)