lua加载csv文件
2015-07-24 16:58
381 查看
文章转自http://www.cnblogs.com/newlist/p/3595739.html -- -- Author: My Name -- Date: 2013-12-16 18:52:11 -- csv解析 -- -- 去掉字符串左空白 local function trim_left(s) return string.gsub(s, "^%s+", ""); end -- 去掉字符串右空白 local function trim_right(s) return string.gsub(s, "%s+$", ""); end -- 解析一行 local function parseline(line) local ret = {}; local s = line .. ","; -- 添加逗号,保证能得到最后一个字段 while (s ~= "") do --print(0,s); local v = ""; local tl = true; local tr = true; while(s ~= "" and string.find(s, "^,") == nil) do --print(1,s); if(string.find(s, "^\"")) then local _,_,vx,vz = string.find(s, "^\"(.-)\"(.*)"); --print(2,vx,vz); if(vx == nil) then return nil; -- 不完整的一行 end -- 引号开头的不去空白 if(v == "") then tl = false; end v = v..vx; s = vz; --print(3,v,s); while(string.find(s, "^\"")) do local _,_,vx,vz = string.find(s, "^\"(.-)\"(.*)"); --print(4,vx,vz); if(vx == nil) then return nil; end v = v.."\""..vx; s = vz; --print(5,v,s); end tr = true; else local _,_,vx,vz = string.find(s, "^(.-)([,\"].*)"); --print(6,vx,vz); if(vx~=nil) then v = v..vx; s = vz; else v = v..s; s = ""; end --print(7,v,s); tr = false; end end if(tl) then v = trim_left(v); end if(tr) then v = trim_right(v); end ret[table.getn(ret)+1] = v; --print(8,"ret["..table.getn(ret).."]=".."\""..v.."\""); if(string.find(s, "^,")) then s = string.gsub(s,"^,", ""); end end return ret; end --解析csv文件的每一行 local function getRowContent(file) local content; local check = false local count = 0 while true do local t = file:read() if not t then if count==0 then check = true end break end if not content then content = t else content = content..t end local i = 1 while true do local index = string.find(t, "\"", i) if not index then break end i = index + 1 count = count + 1 end if count % 2 == 0 then check = true break end end if not check then assert(1~=1) end return content end --解析csv文件 function LoadCsv(fileName) local ret = {}; local file = io.open(fileName, "r") assert(file) local content = {} while true do local line = getRowContent(file) if not line then break end table.insert(content, line) end for k,v in pairs(content) do ret[table.getn(ret)+1] = parseline(v); end file:close() return ret end function Csv2Lua(fileName, filePath, desFilePath ) local t = LoadCsv(filePath); if t then t = luautil.serialize(fileName,t); end if t then luautil.writefile(t, desFilePath) end end --test --local t= LoadCsv("csvtesttxt.csv") --for k,v in pairs(t) do -- local tt = v -- local s = "" -- for i,j in pairs(tt) do -- s = string.format("%s,%s",s,j) -- end -- print ("",s) --end
文章转自http://www.cnblogs.com/newlist/p/3595739.html
相关文章推荐
- mysql load data 导出、导入 csv
- 详解Lua中的表的概念及其相关操作方法
- Lua编程示例(二):面向对象、metatable对表进行扩展
- 把Lua编译进nginx步骤方法
- Lua脚本自动生成APK包
- Lua中的元表(metatable)、元方法(metamethod)详解
- Lua中的metatable介绍
- Lua中ipair和pair的区别
- Lua中的函数精讲笔记
- 浅谈Lua的面向对象特性
- 详解Lua中的变量相关知识点
- Lua脚本语言入门笔记
- Lua脚本调用外部脚本
- 详解Lua中的if语句的使用方法
- Lua中调用函数使用点号和冒号的区别
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中强大的元方法__index详解
- Lua中调用C++函数示例
- Lua面向对象之类和继承浅析
- Lua性能优化技巧(一):前言