我是common-lisp的新手,想知道如何将。csv文件转换为2d列表。我看到我们可以使用
读取。csv文件(cl-csv:read-csv #P"filename.csv")
,你可以转换成二维数组:
(defun read-2d-array (rows &rest args)
(let* ((first-line (apply #'read-line args))
(cols (1+ (count #space first-line)))
(arr (make-array (list rows cols)
:element-type 'integer
:initial-element 0)))
(loop for i below rows
for line = first-line then (apply #'read-line args)
for start = 0
do (dotimes (j cols)
(multiple-value-bind (number end)
(parse-integer line :start start
:junk-allowed t)
(setf start end
(aref arr i j) number))))
arr))
,但是我的输出看起来像上面那个:
#(# #阿 c# l u # # m # # _ # # n n # # m e #, # w # # 我# # d t h #, # l# # # n e g # # t h #, # # r v # e # t我# # c# l # _ # # r #/# f #,# # s t # # 我# # r r u # # p返回# c# 换行符# 1 #,# 9 #,#、1 #、2#, #";#、4 # - #、1 #、2 # d # # 我#,4 # - # # 1 # 0 # d我# # #";#,#、8 # - #、1 #、5 # 0 # c#/# c# 返回# 换行符
,但我希望它看起来像:((column_name width length vertical_r/f stirrup) (c1 9 12 4-12dia 8-150c/c))
给定一个包含
的/tmp/x.csv
文件column_name,width,length,vertical_r/f,stirrup
c1,9,12,4-12dia,8-150c/c
然后使用cl-csv:
> (read-csv #p"/tmp/x.csv")
(("column_name" "width" "length" "vertical_r/f" "stirrup")
("c1" "9" "12" "4-12dia" "8-150c/c"))