lua 工具类2
来源:互联网 发布:js 家谱树形图插件 编辑:程序博客网 时间:2024/05/22 11:49
---- 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) endend--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
阅读全文
0 0
- lua 工具类2
- 转:lua 工具类
- lua工具类
- lua 工具类1
- lua db操作工具类
- lua工具类(一)
- lua工具类(二)
- lua-FileUtils文件工具类
- lua 脚本工具 V1.2
- Lua学习(2), 手工配置Lua的开发工具
- 【Cocos2d-x Lua】异步任务工具类
- 【Cocos2d-x Lua】http工具类封装
- LUA相关工具资源
- LUA相关工具资源
- Lua库和工具
- Lua库和工具
- lua开发工具
- Lua库和工具
- NDK之我淌过的坑
- Web.config或App.config 中添加自定义配置
- 用玩游戏的技巧来解决工作问题
- POJ 2075 Tangled in Cables(最小生成树 kruscal)
- HDU 6148 Valley Numer [数位dp]
- lua 工具类2
- 理解js的函数的执行顺序
- NOIP2017愤怒的小鸟
- Python学习笔记
- java中的单例类型
- Mercurial(Hg)基本操作
- Android官方DataBinding(四):BindingAdapter
- Java基础篇(5)
- 导入项目出现The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path错误