[寒江孤叶丶的Cocos2d-x之旅_24]Cocos2d-x LUA 解析 Csv文件的方法

来源:互联网 发布:吴青峰 知乎 编辑:程序博客网 时间:2024/05/16 16:58

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

博客地址:http://blog.csdn.net/qq446569365

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

ChangeLog:

1.加入数字检测,现在能够转换为数字的都会以数字的形式存储

--[[-------------------------------------------------    CsvLoader.lua    Created 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]) do    print(k,v)end--test2local testarr = loadCsvFile("data/test.csv","EquieID")for k,v in pairs(testarr[1]) do    print(k,v)end--]]_csv_log = function(...)    print(string.format(...))endfunction 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 = 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 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 end    filePath = cc.FileUtils:getInstance():fullPathForFilename(filePath)     local alls = {}    local file = io.open(filePath, "r")    while true do    local line = getRowContent(file)    if not line then            break    end    table.insert(alls, line)    end    local titles = split(alls[1], ",")    for k,v in pairs(titles) do    if string.utf8len(titles[k]) ~= string.len(titles[k]) then        _csv_log("修正前: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)        _csv_log("修正后:titles["..k.."]  "..titles[k].."  "..string.utf8len(titles[k]).."  "..string.len(titles[k]))        end    end    local ID = 1    local arrs = {}    for i = 2, #alls, 1 do        local content = split(alls[i], ",")        local tabelArrs = {}        local index         local flg = false                for j = 1, #titles, 1 do            tabelArrs[titles[j]] = tonumber(content[j]) or dealStr(content[j])            -- print("titles[j]  "..titles[j].."  "..string.utf8len(titles[j]).."  "..string.len(titles[j]))            if indexTitle and indexTitle == titles[j] then            index = tonumber(content[j]) or content[j]            flg = true            end        end        if flg then        arrs[index] = tabelArrs        else        arrs[ID] = tabelArrs        end        ID = ID + 1    end    return arrsend_G.APUtils = _G.APUtils or {}_G.APUtils.LoadCsv = _G.APUtils.loadCsvFile or loadCsvFile




1 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 led顶灯不亮了怎么办 吸顶灯led灯坏了怎么办 车底盘塑料被刮怎么办 汽车门电机坏了怎么办 宁波北仑普高差三分该怎么办 上班的地方甲醛味很重怎么办 公司不给员工交社保怎么办 户口转到学校毕业了怎么办 外地户口转到北京档案怎么办 隧道防水板过紧怎么办 到国企没报到证怎么办 车子锁了油箱盖能开怎么办 单位不接受档案和户口怎么办 完税凭证弄丢了怎么办 育种玉米公本早了怎么办 网银转账打错了怎么办 转账名字打错了怎么办 普通转账名字打错了怎么办 银行账号转错了怎么办 打款信息错了怎么办 报到证过期了档案怎么办 日本suica卡丢了怎么办 日本地铁卡丢了怎么办 极光卡五星老输怎么办 皮秒留下的色沉怎么办 鼻综合鼻内增生怎么办 做完鼻综合脸肿怎么办 上海车子卖了etc怎么办 车卖了etc设备怎么办 车卖了etc没摘怎么办 交通信息卡丢了怎么办 多囊卵巢无排卵怎么办 卵巢腹镜手术后一直出汗怎么办 双侧卵巢多囊怎么办 备孕子宫内膜厚怎么办 有成熟卵泡不破怎么办 卵泡不排萎缩了怎么办 卵泡8mm一直不长怎么办 卵泡已经长到28x19mm怎么办 多囊卵巢卵泡长不大怎么办 优势卵泡19不排怎么办