Skip to content

Instantly share code, notes, and snippets.

@CapsAdmin
Created July 5, 2019 10:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CapsAdmin/3d8c4a7af98df59e78bc2f6e31e9c625 to your computer and use it in GitHub Desktop.
Save CapsAdmin/3d8c4a7af98df59e78bc2f6e31e9c625 to your computer and use it in GitHub Desktop.
csv to table
local function string_split(self, separator)
local tbl = {}
local tbl_i = 1
local last_pos = 1
local len = #separator - 1
for i = 1, #self do
if self:sub(i, i + len) == separator then
tbl[tbl_i] = self:sub(last_pos, i - len - 1)
tbl_i = tbl_i + 1
last_pos = i + len + 1
end
end
tbl[tbl_i] = self:sub(last_pos)
return tbl
end
local function csv2tbl(content, separator, line_separator)
local lines = string_split(content, line_separator)
local keys = string_split(lines[1], separator)
local rows = {}
for i = 2, #lines - 1 do
local values = string_split(lines[i], separator)
local row = {}
for i2 = 1, #values do
row[keys[i2]] = values[i2]
end
rows[i - 1] = row
end
return rows
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment