Lua interface for CSV parser - tsafin/tarantool GitHub Wiki
Tarantool supports CSV file input/output. CSV is comma separated values, like this:
package,method,return value
fio,pathjoin,string
csv,load,table
none,",comma in field", and ""quote""
Commas and linebreaks in fields must be in quotes. Quotes in fields is repeated two times quote character. You can set delimiter and quote character:
opts.delimiter = ','
opts.quote = '"'
Input/output works through readable/writable objects, for example files or sockets.
Readable object has method read(N)
, which returns N
or less bytes as string.
Writable object has method write(string)
, which sends string to output.
Functions
iterate
It's able to iterate over csv file and read line by line.
csv.iterate = function(readable[, opts])
Parameters:
readable
- must be string or object with method read(num) returns stringopts.chunk_size
- Parser will read by chunk_size symbols. Default 4096.opts.delimiter
- Default,
.opts.quote_char
- Default"
.opts.skip_head_lines
- Skip header. Default 0.- Returns iter function, iterator state
Example:
csv = require("csv")
f = require("fio").open("example.txt", { "O_RDONLY"})
for i, tup in csv.iterate(f) do
print(tup[1], tup[2], tup[3])
end
Output:
package method return value
fio pathjoin string
csv load table
none ,comma in field and "quote"
load
Parse csv and make table.
csv.load = function(readable[, opts])
Parameters such as iterate
.
If csv file has a header, it may be skipped, with option skip_head_lines = 1
(if header is just 1 line)
dump
Dump tuple or table as CSV.
csv.dump = function(t[, opts, writable])
Parameters:
t
is tuple or tablewritable
must be object with method write(string) like file or socketopts.delimiter
(default ',').opts.quote_char
(default '"').- It returns CSV as string, if no writeble.
Example:
csv = require("csv")
f = require("fio").open("dump.csv", { "O_WRONLY", "O_TRUNC" , "O_CREAT"}, 0x1FF)
multiline_header = {{'csv example'}, {'3 numbers per string:'}}
csv.dump(multiline_header, nil, f)
for i = 0, 14, 3 do
t = {i, i + 1, i + 2}
s = csv.dump(t, nil, f)
end
dump.csv:
csv example
3 numbers per string:
0,1,2
3,4,5
6,7,8
9,10,11
12,13,14