哈希图返回nil表示现有密钥

时间:2018-08-04 15:25:42

标签: clojure null hashmap key

我有一个csv文件,可以使用clojure.data.csv库进行解析,并将每一行转换为包含字段名称和值的映射。问题是,当我尝试从第一行获取字段qid时,即使打印该行确实显示了正确的哈希图,我也得到了nil。

(ns exam-system.input.input-csv
  (:require [clojure.data.csv :as csv]
            [clojure.java.io :as io]))

(defn csv-data->maps
  [csv-data]
  (map zipmap
       (->> (first csv-data)
            (map keyword)
            repeat)
       (rest csv-data)))

(with-open [reader (io/reader "mypath\\questions.csv")]
  (doall
   (let [row (first (take 1 (csv-data->maps (csv/read-csv reader))))]
     (println "------------")
     (println row)
     (println (get row :qid))
     (println (contains? row :qid)))))

控制台结果:

------------
{:qid 11,001, :question With respect to flight spoilers they :, :answer Option A.     Only operate on the ground.                                                                              
Option B.     Only operate in flight.                                                                                 
Option C.     Can operate both on ground and in flight., :cSubmodule 11.9, :correct Option C.     Can operate both on ground and in flight.}
nil
false

除了明显的部分外,该代码与库的github页中提供的代码基本相同。我也读过this thread,但建议的解决方案不适用。所有其他键都返回适当的值。

更新:示例数据。这些是我文件中的前4个条目。最重要的是我用来制作地图的标题。

qid,question,answer,cSubmodule,correct
"11.001",With respect to flight spoilers they :,"Option A.     Only operate on the ground.                                                                              
Option B.     Only operate in flight.                                                                                 
Option C.     Can operate both on ground and in flight.",11.9,Option C.     Can operate both on ground and in flight.
"11.002",The preferred method of Battery charging a Ni-Cad Battery is constant?,"Option A.     Voltage.                                                                         
Option B.     Current.                                                                             
Option C.     Power.",11.6,Option B.     Current.(CAAIP EEL/1-5 par.4.1)
"11.003","In an aircraft control system, employing servo-tabs installation of external ground locks to main control surface :","Option A.     is unnecessary since system is irreversible and therefore control surface cannot be displace by the wind.                                                                                          
Option B.     would not prevent movement of control column.                                                                                                                           
Option C.     would also prevent movement of control column.",11.9,Option B.     would not prevent movement of control column.(Pallet Anto Flight Control p.222)

1 个答案:

答案 0 :(得分:1)

尽管示例take 1doall的使用是不必要的,但您的示例似乎没有什么错。我可以从您的样本数据中获取价值。我建议利用REPL提供的交互性来一次迈出这一步,并找出它的崩溃原因:

(def row-maps (csv-data->maps (csv/read-csv ...)))

(first row-maps) ;; this should print the first row/map to your REPL
=>
{:qid "11.001",
 :question "With respect to flight spoilers they :",
 :answer "Option A.     Only operate on the ground.                                                                              
          Option B.     Only operate in flight.                                                                                 
          Option C.     Can operate both on ground and in flight.",
 :cSubmodule "11.9",
 :correct "Option C.     Can operate both on ground and in flight."}

(:qid *1)        ;; *1 will contain the value of the previous evaluation
=> "11.001"