Parsing sheets - CSV to Lua table

#1

You can easily export your data from Google Sheets or Microsoft Excel in CSV (comma-separated values) format, but not to a Lua table.

However parsing those files is very easy and I wrote a simple Lua script:

-- 1. Get file path (I put my file here:)
local filename = sys.get_application_path().."\\csv.csv"

-- 2. Create a matcher (set number of columns in your table:)
local columns = 5
local matcher = "%s*(.-)"
for i = 2, columns do
	matcher = matcher..",%s*(.-)"
end

-- 3. Parse the data to a key-value table (specify a key for each column):
local keyTable = {}
for line in io.lines(filename) do
	local a,b,c,d,e = line:match(matcher)
	keyTable[#keyTable + 1] = { key1 = a, key2 = b, key3 = c, key4 = d, key5 = e}
end
pprint(keyTable)
9 Likes

#2

nice

2 Likes