geoffleyland/lua-csv

Having a dump function within lua-csv itself would be nice.

Opened this issue · 1 comments

fcr-- commented

How about something like this?

function dump(row, parameters)
  local separator = (parameter or {}).separator or ','
  local res = {}

  for i, value in ipairs(row) do
    local value = tostring(value)
    if value:find(separator, 1, true) or value:find('"', 1, true) then
      res[i] = ('"%s"'):format(value:gsub('"', '""'))
    else
      res[i] = value
    end
  end
  return table.concat(res, separator)
end

Nice! As you can see, I haven't worked on lua-CSV much for a long time, but if you'd like to make a PR (or even fork the repo and take over), that would be great!