CSV writer only encloses in quotes if needed
This commit is contained in:
parent
f7b3d70bc1
commit
01e6388000
@ -151,11 +151,21 @@ function lzr_csv.write_csv(rows)
|
||||
for r=1, #rows do
|
||||
local values = table.copy(rows[r])
|
||||
for v=1, #values do
|
||||
-- Rule 5: Enclose each value with quotes.
|
||||
-- We enclose ALL values although this is
|
||||
-- not stricly neccessary, but keeps the code simpler.
|
||||
-- Check if field contains a 'special character' (for rule 5)
|
||||
local contains_special_char = string.find(values[v], '[",\013\010]') ~= nil
|
||||
|
||||
-- Rule 7: Quote characters within a value must be escaped
|
||||
values[v] = '"' .. escape_quotes(values[v]) .. '"'
|
||||
if string.find(values[v], '"') then
|
||||
values[v] = escape_quotes(values[v])
|
||||
end
|
||||
-- Rule 5: Field containining linebreak, quotation mark or comma
|
||||
-- need to be enclosed by quotation marks
|
||||
if contains_special_char then
|
||||
-- Note: We *can* enclose every value with quotation marks,
|
||||
-- but only insert them when needed to keep it more
|
||||
-- readable.
|
||||
values[v] = '"' .. values[v] .. '"'
|
||||
end
|
||||
end
|
||||
-- Rule 4: Separate values by comma
|
||||
local output_row = table.concat(values, SEP)
|
||||
|
Loading…
x
Reference in New Issue
Block a user