Created
July 5, 2019 10:56
-
-
Save CapsAdmin/3d8c4a7af98df59e78bc2f6e31e9c625 to your computer and use it in GitHub Desktop.
csv to table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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