从 Clojure 字符串列表中删除额外的转义双引号
Remove extra escaped double quotes from list of Clojure strings
我有一个文件:matches.txt 看起来像这样:
"key1"
"key2"
"key3"
当我读入内存时:
(with-open [r reader "matches.txt")] (doall (line-seq r)))
我得到以下列表结构,其中包含已转义双引号的字符串:
("\"key1\""
"\"key2\""
"\"key3\"")
我们称该结果为 'key-list'。我尝试了很多哈希映射查找,例如:
(test-hash-map (first key-list))
和 none 他们工作。
我想转换这些条目,以便我可以使用它们来检索哈希映射条目。
或者:
- 从内存列表结构中删除转义双引号,或者
- 从 matches.txt 文件中读取不带双引号的字符串。
由于带引号的字符串是字符串的 edn 格式,您可以使用 clojure.edn/read-string 来转换您的字符串,删除引号分隔符(假设您的所有行都用引号分隔):
> (with-open [rdr (clojure.java.io/reader "matches.txt")]
(doall (map clojure.edn/read-string (line-seq rdr))))
("key1" "key2" "key3")
clojure.core/read-string 也可以使用,但请注意
的警告
... read-string can execute code (controlled by read-eval), and as
such should be used only with trusted sources.
我有一个文件:matches.txt 看起来像这样:
"key1"
"key2"
"key3"
当我读入内存时:
(with-open [r reader "matches.txt")] (doall (line-seq r)))
我得到以下列表结构,其中包含已转义双引号的字符串:
("\"key1\""
"\"key2\""
"\"key3\"")
我们称该结果为 'key-list'。我尝试了很多哈希映射查找,例如:
(test-hash-map (first key-list))
和 none 他们工作。
我想转换这些条目,以便我可以使用它们来检索哈希映射条目。
或者:
- 从内存列表结构中删除转义双引号,或者
- 从 matches.txt 文件中读取不带双引号的字符串。
由于带引号的字符串是字符串的 edn 格式,您可以使用 clojure.edn/read-string 来转换您的字符串,删除引号分隔符(假设您的所有行都用引号分隔):
> (with-open [rdr (clojure.java.io/reader "matches.txt")]
(doall (map clojure.edn/read-string (line-seq rdr))))
("key1" "key2" "key3")
clojure.core/read-string 也可以使用,但请注意
的警告... read-string can execute code (controlled by read-eval), and as such should be used only with trusted sources.