The make_csv_parser{ separator="char", delimiter="char", escape=true or "char", trim=true }
function builds a CSV line parser. The make_csv_parser()
function returns a new function that takes a string (e.g. one line from a CSV file) as an argument and returns an array of field values. The returned function splits the string into components according to the provided special CSV charaters. By default, the setting separator
is a comma, the string delimiter
is a double-quote, no escape
character is used (i.e., string delimiters must be stuttered within a string) and extra spaces around the separators are not trim
med. Setting escape
to true uses the backslash character as the escape. Setting trim
to true enables the trimming of whitespace around the separator characters.
For example:
> parse = make_csv_parser()
> T = parse [[a b,"a,b"," a,""b""c", hello "world"!,]]
> for i, v in ipairs(T) do print(i, v) end
1 a b
2 a,b
3 a,"b"c