data.csv-附加到CSV(Clojure)中的新线路

发布于 2025-02-06 05:21:48 字数 814 浏览 1 评论 0原文

我一直在测试此CSV软件包 data.csv

我设法将写作写入CSV。我仍然是Clojure/LISP的新手。

我无法(通过测试或通过文档)找出的是如何在CSV中添加数据。

API文档说了这一点:

write-csv
function
Usage: (write-csv writer data & options)
Writes data to writer in CSV-format.

Valid options are
  :separator (Default \,)
  :quote (Default \")
  :quote? (A predicate function which determines if a string should be quoted. Defaults to quoting only when necessary.)
  :newline (:lf (default) or :cr+lf)

但是我无法锻炼如何使用它。

我正在尝试为以下代码添加一条新行:

(defn writeCSV [name]
  (with-open [writer (io/writer filePath)]
    (csv/write-csv writer
                 [[name 2]])))

name将添加到下一个可用行中。

I have been testing out this csv package data.csv.

I have managed to make the writing to CSV work. I am still very new to clojure/lisp.

What I cannot figure out (through testing or via the documentation), is how to add the data too a new line in the csv.

The API documentation says this:

write-csv
function
Usage: (write-csv writer data & options)
Writes data to writer in CSV-format.

Valid options are
  :separator (Default \,)
  :quote (Default \")
  :quote? (A predicate function which determines if a string should be quoted. Defaults to quoting only when necessary.)
  :newline (:lf (default) or :cr+lf)

But i cannot workout how to use it this.

I am trying to add a new line for the following code:

(defn writeCSV [name]
  (with-open [writer (io/writer filePath)]
    (csv/write-csv writer
                 [[name 2]])))

Where the name is added to the next available line.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

意中人 2025-02-13 05:21:48

使用:append io/writer的选项:

(require
  '[clojure.data.csv :as csv] 
  '[clojure.java.io :as io])

(with-open [writer (io/writer "/tmp/foo.csv" :append true)] 
  (csv/write-csv writer [["name" 1]]))
(with-open [writer (io/writer "/tmp/foo.csv" :append true)] 
  (csv/write-csv writer [["name" 2]]))

(println (slurp "/tmp/foo.csv"))
;;=>
name,1
name,2

Use the :append option of io/writer:

(require
  '[clojure.data.csv :as csv] 
  '[clojure.java.io :as io])

(with-open [writer (io/writer "/tmp/foo.csv" :append true)] 
  (csv/write-csv writer [["name" 1]]))
(with-open [writer (io/writer "/tmp/foo.csv" :append true)] 
  (csv/write-csv writer [["name" 2]]))

(println (slurp "/tmp/foo.csv"))
;;=>
name,1
name,2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文