x LUA 解析 Csv文件的方法

原创文章,欢迎转载,转载请注明:文章来自[寒江孤叶丶的Cocos2d-x之旅系列]

博客地址:

之前项目用CocoSutdio的数据编辑器导出的Json作为数据文件读取到游戏中,后来发现,效率太低的……低的可怜啊,于是开始选用Excel导出的CSV文件。从挖来了一段代码,但是存在一些不适用于被项目的问题。于是着手解决了两个主要问题1.CSV字符串换行的问题,2.第一个字段名有看不见的三个byte的问题。将代码分享出来,希望对大家有帮助

–[[————————————————-CsvLoader.luaCreated by ArcherPeng on 15-03-18.————————————————-功能概述:本模块用于读取Excel导出的CSV文件,将读取的结果保存在一个table中本模块支持换行功能,但需要特殊标记在字符串开头加入#rrr表示开启换行,之后每加入一个#rrr则会换一次例如:#rrr普通副本#rrr挑战副本#rrr挂机副本#rrr日常副本#rrr结果为“普通副本挑战副本挂机副本日常副本”loadCsvFile(filePath,indexTitle)filePath 文件的路径indexTitle 作为teble的Key的字段————————————————-示例代码:–test1local testarr = loadCsvFile("data/test.csv")for k,v in pairs(testarr[1]) doprint(k,v)end–test2local testarr = loadCsvFile("data/test.csv","EquieID")for k,v in pairs(testarr[1]) doprint(k,v)end–]]function split(str, reps)local resultStrsList = {};string.gsub(str, '[^' .. reps ..']+', function(w) table.insert(resultStrsList, w) end );return resultStrsList;endlocal function getRowContent(file)local content;local check = falselocal count = 0while true dolocal t = file:read()if not t thenif count == 0 thencheck = trueendbreakendif not content thencontent = telsecontent = content..tendlocal i = 1while true dolocal index = string.find(t, "\&;", i)if not index then break endi = index + 1count = count + 1endif count % 2 == 0 thencheck = truebreakendendif not check thenassert(1~=1)endreturn content and (string.trim(content))end–处理str是否需要换行,,在字符串前加#rrr表示开启换行,然后所有需要换行的地方加入#rrrlocal function dealStr(str)if type(str) ~= "string" then return strendlocal keyWord = string.sub(str,1,4)if keyWord ~= "#rrr" thenreturn strend str = string.sub(str,5)local strArr = string.split(str,"#rrr")return table.concat(strArr,"\n")endfunction loadCsvFile(filePath,indexTitle)if not filePath then return endfilePath = cc.FileUtils:getInstance():fullPathForFilename(filePath)local alls = {}local file = io.open(filePath, "r")while true dolocal line = getRowContent(file)if not line thenbreakendtable.insert(alls, line)endlocal titles = split(alls[1], ",")for k,v in pairs(titles) doif string.utf8len(titles[k]) ~= string.len(titles[k]) thencclog("修正前:titles["..k.."] "..titles[k].." "..string.utf8len(titles[k]).." "..string.len(titles[k]))titles[k] = string.sub(titles[k],string.len(titles[k]) – string.utf8len(titles[k]) + 2)cclog("修正后:titles["..k.."] "..titles[k].." "..string.utf8len(titles[k]).." "..string.len(titles[k]))endendlocal ID = 1local arrs = {}for i = 2, #alls, 1 dolocal content = split(alls[i], ",")local tabelArrs = {}local indexlocal flg = falsefor j = 1, #titles, 1 dotabelArrs[titles[j]] = dealStr(content[j])– print("titles[j] "..titles[j].." "..string.utf8len(titles[j]).." "..string.len(titles[j]))if indexTitle and indexTitle == titles[j] thenindex = content[j]flg = trueendendif flg thenarrs[index] = tabelArrselsearrs[ID] = tabelArrsendID = ID + 1endreturn arrsend_G.APUtils = _G.APUtils or {}_G.APUtils.LoadCsv = _G.APUtils.loadCsvFile or loadCsvFile

只想到处流浪人生就像一场旅行,不必在乎目的地,

x LUA 解析 Csv文件的方法

相关文章:

你感兴趣的文章:

标签云: