lua.wetgenes.csv
local wcsv = require("wetgenes.csv")
Load and save csv, prefrably using tab sperators.
The following need to be escaped with a \ when used in each column.
\n for newline,
\t for tab,
\r for carriage return,
\\ for backslash.
and when using commas a , must be placed inside a quoted string with a double "" to escape " within this string.
This is intended for "small" csv files that fit in memory so does not stream or try and do anything clever to reduce memory overheads.
lua.wetgenes.csv.build
Build csv data into a string from a simple table of lines where each line is a table of cells.
text = wcsv.build(lines)
text = wcsv.build(lines,opts)
lua.wetgenes.csv.doesc
Escape special chars within a csv cell.
lua.wetgenes.csv.doquote
Wrap a string in quotes and escape any " within that string using csv rules.
lua.wetgenes.csv.map
Use the first line to map all other lines into named keys, an empty string will map to nil. This will return an array of items that is smaller than the array of lines by at least one as we also trim trailing empty objects.
items = wcsv.map(lines)
items = wcsv.map(wcsv.parse(text))
lua.wetgenes.csv.parse
Parse csv data from a chunk of text. Returns a simple table of lines where each line is a table of cells. An empty or missing string indicates an empty cell. The second return can be ignored or used to build a csv in a similar format to the one we read.
lines,opts = wcsv.parse(text)
lines,opts = wcsv.parse(text,opts)
Opts can be used to control how the parsing is performed pass in a seperator value to contol how a line is split.
lines,opts = wcsv.parse(text,{seperator=","})
Note that we also return the seperator we used within the second return and will guess the right one using the first line if one is not given.
lua.wetgenes.csv.unesc
Unescape special chars within a csv cell.
lua.wetgenes.csv.unquote
Remove quotes from a strine and unescape any "" within that string. If the string is not in quotes then we return it as is.