-
Notifications
You must be signed in to change notification settings - Fork 387
Lua interface for CSV parser
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.
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 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:
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"
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 tuple or table as CSV.
csv.dump = function(t[, opts, writable])
Parameters:
-
t
is tuple or table -
writable
must be object with method write(string) like file or socket -
opts.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
- C coding guidelines ↗
- Lua coding guidelines ↗
- Python coding guidelines ↗
- Maintainer's guide
- Debugging
Architecture
- Server architecture
- R tree index quick start and usage
- LuaJIT
- Vinyl
- Vinyl Architecture
- Vinyl Disk Layout
- Vinyl math
- Vinyl Cookbook
- Bullet1
- SQL
- Appserver modules
- Testing
- Performance
- Privileges and Access control
How To ...?
- ... configure build system
- ... add new fuzzers
- ... build RPM or Deb package using packpack
- ... calculate memory size
- ... debug core dump of stripped tarantool
- ... debug core from different OS
- ... debug fuzzer
- ... generate new bootstrap snapshot
- ... use Address Sanitizer
- ... collect a coredump
- ... generate luacov report for builtin module
- ... verify modified lua files via luacheck
- ... verify Lua files in third_party?
- ... rerun failed jobs
- ... update a third party repository
- Fix wrong decimal indexing after upgrade to 2.10.1
- Caveats when upgrading a cluster on Tarantool 1.6
- Fix illegal field type in a space format when upgrading to 2.10.4
Useful links