如何从文本文件中读取字符串并在 clojure 中转换为地图



我正在尝试使用 Clojure - Seesaw 从文件中读取并将字符串转换为映射(变量),以便我可以使用它们打印到 GUI。以下是我当前的代码:

(ns store.core
(:gen-class)
(:require [seesaw.core :as seesaw]))
(defn -main
  [& args]
  (seesaw/show!
   (spit "amovies.txt" "")
   ;(spit "amovies.txt" (pr-str [{:id 1 :qty 4 :name "movie1" :price 3.50}
   ;                             {:id 2 :qty 5 :name "movie2" :price 3.00}]) :append true)
   (spit "amovies.txt" "movie: Movie_Namenprice: 5nid: 1nquantity: 2" :append true)
   (print (read-string (slurp "amovies.txt")))
   (with-open [rdr (reade "amovies.txt")]
     (doseq [line (line-seq rdr)]
       (println-str line)))

我一直在弄清楚如何逐行从电影中读取字符串.txt然后用它创建一个地图。所需的输出应类似于电影:Movie_Name价格: 5编号: 1数量: 2

但在某种程度上,如果我说:movie,它会引用电影的名称。

有人可以帮忙吗?感谢所有帮助!

1

(def movies-as-map
  (let [lines (with-open [rdr (reade "amovies.txt")]
                (line-seq rdr))]
    (binding [*read-eval* false]
      (map read-string lines))))

阿拉伯数字

user> (def movie
        (binding [*read-eval* false]
          (read-string 
           "{:id 1 :qty 4 :name "movie1" :price 3.50}")))
#'user/movie
user> (keys movie)
(:id :qty :name :price)

将与此一起使用

(spit "amovies.txt" 
      (pr-str [{:id 1 :qty 4 :name "movie1" :price 3.50}
               {:id 2 :qty 5 :name "movie2" :price 3.00}]) 
      :append true)

但不是这个

(spit "amovies.txt" 
      "movie: Movie_Namenprice: 5nid: 1nquantity: 2"
      :append true)

文档:https://clojure.github.io/clojure/branch-master/clojure.core-api.html#clojure.core/read-string

将错误的参数传递给show!

看起来你传递了大量的论点来跷跷板/展示!

文档指出

用法:(显示!

显示框架、对话框或小部件。

如果目标是模态对话框,则调用将阻止并显示!返回对话框的结果。请参阅(seesaw.core/return-from-dialog)。

返回其输入。

相关内容

  • 没有找到相关文章

最新更新