是否可以从输入文件读取并将文本文件的每一行转换为字符串向量?

时间:2019-05-29 02:31:42

标签: clojure

我有一个文本文件,如下所示:

---#--###----
-#---#----##-
####-#-#-#-##
---#---#-#---
-#-####---##-
-#------#----
-############
------------@

我可以从输入文件中读取并获得这种格式的输出

[ ["-" "-" "-" "#" "-" "-" "#" "#" "#" "-" "-" "-" "-"] 
  ["-" "#" "-" "-" "-" "#" "-" "-" "-" "-" "#" "#" "-"] 
  ["#" "#" "#" "#" "-" "#" "-" "#" "-" "#" "-" "#" "#"] 
  ["-" "-" "-" "#" "-" "-" "-" "#" "-" "#" "-" "-" "-"] 
  ["-" "#" "-" "#" "#" "#" "#" "-" "-" "-" "#" "#" "-"] 
  ["-" "#" "-" "-" "-" "-" "-" "-" "#" "-" "-" "-" "-"] 
  ["-" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#"] 
  ["-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "@"]
]

我尝试逐行读取输入文件并添加到向量中:

    (defn loadFile2 []
        (with-open [rdr (clojure.java.io/reader "map.txt")]
        (reduce conj [] (line-seq rdr)))
    )

    (defn convert_input_to_string []
        "Converts"
        ;(def temp )
        (loop [i 0 temp (loadFile2)]
            (if (< i (count temp))

            (recur (inc i) (assoc temp i (str (join (seq (get temp 
             i))))) )
            temp
            ))
    )

(convert_input_to_string).

这种方法给了我一个向量字符串:

[---#--###---- 
 -#---#----##- 
 ####-#-#-#-## 
 ---#---#-#--- 
 -#-####---##- 
 -#------#---- 
 -############ 
 ------------@
]

我希望矢量采用以下格式:

[ ["-" "-" "-" "#" "-" "-" "#" "#" "#" "-" "-" "-" "-"] 
  ["-" "#" "-" "-" "-" "#" "-" "-" "-" "-" "#" "#" "-"] 
  ["#" "#" "#" "#" "-" "#" "-" "#" "-" "#" "-" "#" "#"] 
  ["-" "-" "-" "#" "-" "-" "-" "#" "-" "#" "-" "-" "-"] 
  ["-" "#" "-" "#" "#" "#" "#" "-" "-" "-" "#" "#" "-"] 
  ["-" "#" "-" "-" "-" "-" "-" "-" "#" "-" "-" "-" "-"] 
  ["-" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#"] 
  ["-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "@"]]

3 个答案:

答案 0 :(得分:1)

一步一步来

读取数据(因为文件很小,所以只需lur一下即可)。我们将文件命名为grid.txt

(slurp "grid.txt")
"---#--###----\r\n-#---#----##-\r\n####-#-#-#-##\r\n---#---#-#---\r\n-#-####---##-\r\n-#------#----\r\n-############\r\n------------@"

分成几行:

(->> (slurp "grid.txt") 
     (clojure.string/split-lines))
["---#--###----" "-#---#----##-" "####-#-#-#-##" "---#---#-#---" "-#-####---##-" "-#------#----" "-############" "------------@"]

将每个字符串转换为字符向量:

(->> (slurp "grid.txt") 
     (clojure.string/split-lines) 
     (map vec))
([\- \- \- \# \- \- \# \# \# \- \- \- \-] [\- \# \- \- \- \# \- \- \- \- \# \# \-] [\# \# \# \# \- \# \- \# \- \# \- \# \#] [\- \- \- \# \- \- \- \# \- \# \- \- \-] [\- \# \- \# \# \# \# \- \- \- \# \# \-] [\- \# \- \- \- \- \- \- \# \- \- \- \-] [\- \# \# \# \# \# \# \# \# \# \# \# \#] [\- \- \- \- \- \- \- \- \- \- \- \- \@])

将顶层转换为向量:

(->> (slurp "grid.txt") 
     (clojure.string/split-lines) 
     (map vec) 
     (into []))
[[\- \- \- \# \- \- \# \# \# \- \- \- \-] [\- \# \- \- \- \# \- \- \- \- \# \# \-] [\# \# \# \# \- \# \- \# \- \# \- \# \#] [\- \- \- \# \- \- \- \# \- \# \- \- \-] [\- \# \- \# \# \# \# \- \- \- \# \# \-] [\- \# \- \- \- \- \- \- \# \- \- \- \-] [\- \# \# \# \# \# \# \# \# \# \# \# \#] [\- \- \- \- \- \- \- \- \- \- \- \- \@]]

请注意,这与预期的输出不同,因为内部向量包含字符,而不是字符串。

如果您确实想要字符串,则需要添加一行:

(->> (slurp "grid.txt") 
     (clojure.string/split-lines) 
     (map #(map str %)) 
     (map vec) 
     (into []))
[["-" "-" "-" "#" "-" "-" "#" "#" "#" "-" "-" "-" "-"] ["-" "#" "-" "-" "-" "#" "-" "-" "-" "-" "#" "#" "-"] ["#" "#" "#" "#" "-" "#" "-" "#" "-" "#" "-" "#" "#"] ["-" "-" "-" "#" "-" "-" "-" "#" "-" "#" "-" "-" "-"] ["-" "#" "-" "#" "#" "#" "#" "-" "-" "-" "#" "#" "-"] ["-" "#" "-" "-" "-" "-" "-" "-" "#" "-" "-" "-" "-"] ["-" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#"] ["-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "@"]]

答案 1 :(得分:0)

如果您有能力将整个输入文件加载到内存中,则可以使用以下快捷功能:

(defn read-vec-of-strings [filename]
  (let [contents (slurp filename)                      ;; see (1) below
        lines    (clojure.string/split contents #"\n") ;; see (2) below
        vectors  (mapv #(mapv str %) lines)]           ;; see (3) below
    vectors))
  • 步骤(1)将整个文件作为单个字符串加载到内存中;
  • 步骤(2)用换行符分割文件;
  • 步骤(3)是发生魔术的地方:对于文件(mapv ... lines)中的每一行,调用另一个(匿名)函数#(mapv str %),该函数遍历该行的每个字符并将其转换为字符串(因此字符\#变成字符串"#"),并将结果累加到向量中。

我将您的输入文件另存为input.txt,并调用了这样的函数:

>  (clojure.pprint/pprint (read-vec-of-strings "input.txt"))

[["-" "-" "-" "#" "-" "-" "#" "#" "#" "-" "-" "-" "-"]
 ["-" "#" "-" "-" "-" "#" "-" "-" "-" "-" "#" "#" "-"]
 ["#" "#" "#" "#" "-" "#" "-" "#" "-" "#" "-" "#" "#"]
 ["-" "-" "-" "#" "-" "-" "-" "#" "-" "#" "-" "-" "-"]
 ["-" "#" "-" "#" "#" "#" "#" "-" "-" "-" "#" "#" "-"]
 ["-" "#" "-" "-" "-" "-" "-" "-" "#" "-" "-" "-" "-"]
 ["-" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#" "#"]
 ["-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "-" "@"]]
nil

答案 2 :(得分:0)

最简单的是这样的:

(with-open [rdr (clojure.java.io/reader "data.txt")]
  (mapv vec (line-seq rdr)))

或者如果您需要字符串而不是字符:

(with-open [rdr (clojure.java.io/reader "data.txt")]
  (mapv #(clojure.string/split % #"") (line-seq rdr)))

元素向量均返回向量