Skip to content

Lua interface for CSV parser

Daniil Medvedev edited this page Aug 31, 2015 · 8 revisions

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 an 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. lua csv.iterate = function(readable[, opts]) Parameters:

  • readable - must be string or object with method read(num) returns string
  • opts.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:

    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

csv.load = function(readable[, opts]) --@brief parse csv and make table --@param opts like in iterate --@return table If csv file has a header, it may be skipped, with option skip_head_lines = 1 (if header is just 1 line)

csv.dump = function(t[, opts, writable]) --@brief dumps tuple or table as csv --@param t is tuple or table --@param writable must be object with method write(string) like file or socket --@param opts.delimiter (default ','). --@param opts.quote_char (default '"'). --@return there is no writable it returns csv as string

Example: 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, f) for i=0,14,3 do t = {i, i + 1, i + 2} s = csv.dump(t, 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

Clone this wiki locally