【问题标题】:Hash-map returns nil for existing key哈希映射为现有键返回 nil
【发布时间】:2018-08-04 15:25:41
【问题描述】:

我有一个 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)

【问题讨论】:

  • 已添加,我确实将第 4 位的第一列的逗号更改为“。”以防万一,但没有改变
  • 无法复制。当我运行它时,我得到11.001true。另外,你为什么使用doall?我看不到在那里做任何有用的事情。
  • 我使用 doall 的唯一原因是让我的代码与库页面中提供的代码接近,尝试删除它但没有改变任何东西
  • 我会删除它。 doall 强制传递给它的惰性列表进行全面评估。我认为它没有抛出错误的唯一原因是 let 返回 nil,在某些情况下可以将其视为(空)集合。
  • 原始代码是(doall (csv/read-csv reader))),这确实有意义,因为正在传递一个集合。不过这里没有必要,因为无论如何你只是抓取第一个元素。

标签: clojure null hashmap key


【解决方案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"

【讨论】:

  • 我在我的 repl 中复制了整个过程,但仍然得到相同的结果。有没有可能是一些奇怪的角色把这搞砸了?我的 csv 文件曾经是我保存为 csv 的 excel 文件。
  • 好吧,我是对的。我使用我提供的示例运行我的代码并且它可以工作。很抱歉耽误您的时间
猜你喜欢
  • 2011-09-29
  • 2021-06-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多