lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

>Does anyone have a nice piece of Lua code that would parse a CSV (Comma
>Separated Values) file ?

To parse a general CSV file, in which commas may appear inside quoted strings,
is hard to do in a "natural" way in Lua, ie, using gsub or strfind.
(I think Roberto tried to do this once.)
I can't see any other way than doing a character-by-character parsing.

If there are no quoted strings, or no commas inside quoted strings, then it's
simple. Something like
	local t={}
	gsub(s..",","([^,]*),",function (x) tinsert(%t,x) end)