FourierTransformer / ftcsv

a fast csv library written in pure Lua
MIT License
73 stars 20 forks source link

headers = false is ignored when using ftcsv.parse() #31

Closed agladysh closed 1 year ago

agladysh commented 1 year ago
a, b,
1, 2, 3
local ftcsv = require 'ftcsv'

local data = assert(ftcsv.parse('example.csv', ',', { headers = false }))

print(assert(ftcsv.encode(data, ',')))
% lua test2.lua
lua: /usr/local/share/lua/5.4/ftcsv.lua:335: ftcsv: Cannot parse a file which contains empty headers
stack traceback:
    [C]: in function 'error'
    /usr/local/share/lua/5.4/ftcsv.lua:335: in upvalue 'handleHeaders'
    /usr/local/share/lua/5.4/ftcsv.lua:524: in upvalue 'parseHeadersAndSetupArgs'
    /usr/local/share/lua/5.4/ftcsv.lua:546: in function 'ftcsv.parse'
    test2.lua:3: in main chunk
    [C]: in ?
FourierTransformer commented 1 year ago

Hey! It looks like your example is not really a valid CSV, so ftcsv is not going to be able to parse it. You could always strip out the first row if needed, and then parse without headers. I suppose the error message could be improved to indicate that an empty value was found for a header in the first row.

The headers=false is for parsing header-less files, but the first row will still get parsed and put into the parsed data structure. So instead of data[1].a = 1, where a is the name of the header, it'll parse using numbers so data[1][1] = 'a' and data[2][1] = 1

Hope this helps!

FourierTransformer commented 1 year ago

Actually, I take that back. This is an interesting bug. With that specific setup, data[1][3] should come in empty. I'll see about getting this fixed.