Home

Awesome

Lua-CSV - delimited file reading

1. What?

Lua-CSV is a Lua module for reading delimited text files (popularly CSV and tab-separated files, but you can specify the separator).

Lua-CSV tries to auto-detect whether a file is delimited with commas or tabs, copes with non-native newlines, survives newlines and quotes inside quoted fields and offers an iterator interface so it can handle large files.

2. How?

local csv = require("csv")
local f = csv.open("file.csv")
for fields in f:lines() do
  for i, v in ipairs(fields) do print(i, v) end
end

csv.open takes a second argument parameters, a table of parameters controlling how the file is read:

csv.openstring works exactly like csv.open except the first argument is the contents of the csv file. In this case buffer_size is set to the length of the string.

3. Requirements

Lua 5.1, 5.2 or LuaJIT.

4. Issues

5. Wishlist

6. Alternatives