错误:在文件写入中尝试对布尔值进行索引

我想将两个矩阵分别分成两个 csv 文件,比如 1.csv 和 2.csv

data = [[
        1 2 3
        4 5 6
        7 8 9

    2 3 4
    5 7 8
    9 1 2

    ]]
--------------------------------------------------------------------------------
local cc = table.concat
--------------------------------------------------------------------------------
function save_csv(filenumber,t)
  io.open(filenumber .. '.csv','w'):write(cc(t,'\n')):close()
end
--------------------------------------------------------------------------------
local filenumber = 1
local ans = {}
for line in (data..'\n'):gmatch '(.-)\n' do
  local s = {}
  for item in line:gmatch '%d+' do s[#s+1] = item end
  if #s > 0 then
    ans[#ans+1] = cc(s,',')
  elseif #ans > 0 then
    save_csv(filenumber,ans)
    filenumber = filenumber + 1
    ans = {}
  end
end

在以上代码后,只生成了 1.csv,并提示错误:尝试对布尔值进行索引 调用堆栈: 1.lua:22: 在函数 'save_csv' 中 1.lua:34: 在程序的主块中,我认为数据可能有一些错误,有谁能帮帮我吗?

点赞