Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module(..., package.seeall)
- ---------------------------------------------------------------------
- local function split(str, sep)
- sep = sep or ','
- fields={}
- local matchfunc = string.gmatch(str, "([^"..sep.."]+)")
- if not matchfunc then return {str} end
- for str in matchfunc do
- table.insert(fields, str)
- end
- return fields
- end
- ---------------------------------------------------------------------
- function read(path, sep, tonum)
- tonum = tonum or true
- sep = sep or ','
- local csvFile = {}
- local file = assert(io.open(path, "r"))
- for line in file:lines() do
- fields = split(line, sep)
- if tonum then -- convert numeric fields to numbers
- for i=1,#fields do
- fields[i] = tonumber(fields[i]) or fields[i]
- end
- end
- table.insert(csvFile, fields)
- end
- file:close()
- return csvFile
- end
- ---------------------------------------------------------------------
- function write(path, data, sep)
- sep = sep or ','
- local file = assert(io.open(path, "w"))
- for i=1,#data do
- for j=1,#data[i] do
- if j>1 then file:write(sep) end
- file:write(data[i][j])
- end
- file:write('\n')
- end
- file:close()
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement