【发布时间】: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.001和true。另外,你为什么使用doall?我看不到在那里做任何有用的事情。 -
我使用 doall 的唯一原因是让我的代码与库页面中提供的代码接近,尝试删除它但没有改变任何东西
-
我会删除它。
doall强制传递给它的惰性列表进行全面评估。我认为它没有抛出错误的唯一原因是let返回nil,在某些情况下可以将其视为(空)集合。 -
原始代码是
(doall (csv/read-csv reader))),这确实有意义,因为正在传递一个集合。不过这里没有必要,因为无论如何你只是抓取第一个元素。