如何使用空格分隔键访问值? [英] How to access a value using a space-separated key?
问题描述
当我有空格分隔键时,如何使用它来提取值而不重新创建键?
When I have a space-separated key, how can I use it to extract a value without re-creating the key?
我有一组潜在的键,列名实际上作为从clojure-csv返回的数据中的第一个序列:(这是格式化以避免滚动。)
I have a set of potential keys, column names actually, as the first sequence in data returned from clojure-csv: (This is formatted to avoid scrolling.)
["AGY/DIV " "STS" "GIC-ID "
"LAST-NAME "
"FIRST-NAME "
"COVERAGE DESCRIPTION "
"PREMIUM "
"RUN-DATE"
"BIL MO "]
行和zipmap的键与每个后续行(序列)的数据:
Then I create keys from this row and zipmap the keys with each subsequent row (sequence) of data:
(defn create-map-keys
"Takes a sequence, and turns it into viable keys for a map."
[in-seq]
(map (fn [element] (keyword element)) (map #(cstr/trim %1) in-seq)))
; gic-csv-keys
(:AGY/DIV :STS :GIC-ID :LAST-NAME :FIRST-NAME
:COVERAGE DESCRIPTION :PREMIUM :RUN-DATE :BIL MO)
(defn zip-apply-col-nams
[col-keys row]
(zipmap col-keys row))
对于测试数据,我将键映射到csv-data的第二行。
For test data, I zipmap the keys to the second row of the csv-data.
(def zm2 (zip-apply-col-nams gic-csv-keys (first gic-csv-data)))
当我尝试从:COVERAGE DESCRIPTION
键中提取一个值时,得到此错误
When I try to extract a value from the :COVERAGE DESCRIPTION
key, I get this error
(:COVERAGE DESCRIPTION zm2)
CompilerException java.lang.RuntimeException: Unable to resolve symbol: DESCRIPTION in this context, compiling:(NO_SOURCE_PATH:23)
但是,这样工作:
(zm2 (keyword "COVERAGE DESCRIPTION"))
"HARVARD PILGRIM FAMILY - INSURED "
我应该修改空格分隔的键以替换空格,例如,一个破折号,还是有另一种方法来引用键而不重新创建它?
Should I be modifying space-separated keys to replace the space with, for example, a dash, or is there another way to refer to the key without recreating it?
感谢。
推荐答案
记录:某些键关键字在Clojure。您应该用 - 或_替换空格。或者你应该使用纯字符串作为键,并使用 get
。
For the record: "some key" is (at the moment) not a valid keyword in Clojure. You should either replace the whitespace with - or _. Or you should go with plain strings as keys and use get
.
这篇关于如何使用空格分隔键访问值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!