• Stars
    star
    122
  • Rank 292,031 (Top 6 %)
  • Language
    Lua
  • License
    MIT License
  • Created almost 11 years ago
  • Updated over 9 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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:

  • separator sets the separator. It'll probably guess the separator correctly if it's a comma or a tab (unless, say, the first field in a tab-delimited file contains a comma), but if you want something else you'll have to set this. It could be more than one character, but it's used as part of a set: "["..sep.."\n\r]"

  • Set header to true if the file contains a header and each set of fields will be keyed by the names in the header rather than by integer index.

  • columns provides a mechanism for column remapping. Suppose you have a csv file as follows:

      Word,Number
      ONE,10
    

    And columns is:

    • { word = true } then the only field in the file would be { word = "ONE" }

    • { first = { name = "word"} } then it would be { first = "ONE" }

    • { word = { transform = string.lower }} would give { word = "one" }

    • finally,

        { word = true
          number = { transform = function(x) return tonumber(x) / 10 end }}
      

      would give { word = "ONE", number = 1 }

    A column can have more than one name: { first = { names = {"word", "worm"}}} to help cope with badly specified file formats and spelling mistakes.

  • buffer_size controls the size of the blocks the file is read in. The default is 1MB. It used to be 4096 bytes which is what pagesize says on my system, but that seems kind of small.

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

  • Some whitespace-delimited files might use more than one space between fields, for example if the columns are "manually" aligned:

      street           nr  city
      "Oneway Street"   1  Toontown
    

    It won't cope with this - you'll get lots of extra empty fields.

5. Wishlist

  • Tests would be nice.
  • So would better LDoc documentation.

6. Alternatives

  • Penlight contains delimited file reading. It reads the whole file in one go.
  • The Lua Wiki contains two pages on CSV here and here.
  • There's an example using LPeg to parse CSV here

More Repositories

1

luatrace

A tool for tracing Lua script execution and analysing time profiles and coverage
Lua
168
star
2

lua-mmapfile

A simple interface to mmap for writing and reading binary data
Lua
24
star
3

rima

Rima is a tool for formulating mathematical models
Lua
19
star
4

lua-heaps

Binary and skew heaps
Lua
17
star
5

lua-kdtree

n-dimensional kdtree spatial indexing
Lua
17
star
6

luasimplex

The simplex method for solving linear programming problems implemented in Lua
Lua
15
star
7

lua-geometry2d

Some 2D geometry algorithms in Lua
Lua
14
star
8

lua-osm-tools

Tools for reading OpenStreetMap (OSM) data and gradually turning it into routable networks.
Lua
13
star
9

VRPH

A fork of COIN's VRPH
C++
11
star
10

py-gdx

py-gdx is a small set of utilities for handling GAMS GDX files from Python
Python
11
star
11

argcheck

Lua tool for checking function arguments against specifications parsed from comments
Lua
9
star
12

lua-readosm

Lua binding to readosm - a library for reading OpenStreetMap XML and PBF files
Lua
9
star
13

lua-shapefile

Read ESRI Shapefiles in Lua
Lua
7
star
14

lua-patengi

Yet more database connectivity for Lua
Lua
5
star
15

heroku-buildpack-openresty-template

Template project for openresty on Heroku
JavaScript
4
star
16

heroku-buildpack-openresty

Can I build openresty and luarocks from source?
Shell
4
star
17

lua-dbf

dbf reading for Lua
Lua
3
star
18

lua-proj

Lua binding to Proj.4
Lua
3
star
19

lua-gdx

lua-gdx is a small set of utilities for handling GAMS GDX files from Lua
Lua
3
star
20

assignment

A C implementation of the Hungarian algorithm for the assignment problem
C
3
star