-
Notifications
You must be signed in to change notification settings - Fork 88
/
Copy pathcsv.lua
79 lines (74 loc) · 1.83 KB
/
csv.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
local concat = table.concat
local type = type
local csv = {}
-- 读取并且解析CSV文件, 格式如下:
--[[
[1] = [Name1, Name2, Nam3, ..., NameN]
[2] = [Value1, Value2, Value3, ..., ValueN]
.
..
...
[N] = [Value1, Value2, Value3, ..., ValueN]
]]
-- 规则1: 第一行为所有字段的名称, 第二行开始到第N行是内容;
-- 规则2: 每行不允许出现空格与逗号引号等特殊字符, 空格字符将会被删除掉;
-- 规则3: 打开csv文件失败将返回nil与一个err错误信息.
function csv.loadfile (path)
if type(path) ~= 'string' or path == '' then
return nil, "invalid args."
end
local file, err = io.open(path, "r")
if not file then
return nil, err
end
local tab = {}
for line in file:lines() do
local items = {}
for item in line:gmatch("([^,\r\n]+)") do
if item and item ~= '' then
items[#items + 1] = item:gsub("[ \"']", "")
end
end
if #items > 0 then
tab[#tab + 1] = items
end
end
file:close()
return tab
end
-- 规则同上
function csv.writefile (path, t)
if type(path) ~= 'string' or path == '' or type(t) ~= 'table' or #t < 1 then
return nil, "invalid args."
end
local file, err = io.open(path, "w")
if not file then
return nil, err
end
file:setvbuf("full", 1 << 20)
for index = 1, #t do
local contents = t[index]
if type(contents) == 'table' then
file:write(concat(contents, ',') .. '\n')
end
end
file:flush()
file:close()
return true
end
function csv.loadstring (str)
if type(str) ~= 'string' or str == '' then
return nil, "invalid args."
end
local tab = {}
local index = 1
for line in str:gmatch("([^\r\n]-)\r\n") do
local items = {}
for s in line:gmatch("([^, \r\n]+)") do
items[#items+1] = s
end
tab[#tab+1] = items
end
return tab
end
return csv