csvtool = {}

function csvtool:csv2table(filename)
if type(filename) ~= "string" or filename == "" then
print("the filename is not a string or a empty string")
return nil
end
local filepath = cc.FileUtils:getInstance():fullPathForFilename(filename)
local filedate = cc.FileUtils:getInstance():getStringFromFile(filepath)
print(filedate)
local dataByLine = Split(filedate,'\n')
local titleArray = Split(dataByLine[],',')
local lineIndex =
local titleSize = table.getn(titleArray)
local returnTable = {}
for line = ,table.getn(dataByLine)- do
local dataByLien_split = Split(dataByLine[line],',')
returnTable[lineIndex] = {}
for i = ,titleSize do
returnTable[lineIndex][titleArray[i]] = dataByLien_split[i]
end
lineIndex = lineIndex+
end
return returnTable
end function csvtool:returnCsvTableTitle(csvtable)
local titleTable = {}
local titleIndex =
for key, var in pairs(csvtable[]) do
titleTable[titleIndex] = key
titleIndex = titleIndex +
end
return titleTable
end function csvtool:returnScvTitleIndex(csvtable,findtitlename)
if type(csvtable) ~= "table" or table.getn(csvtable) == then
print("the argment 1 is not a csvtable or is a empty table")
return nil
end
if findtitlename == "" or type(findtitlename) ~= "string" then
print("the findtitlename is a empty string or the type not a string")
return nil
end
local csvtitle = csvtool:returnCsvTableTitle(csvtable)
for key, var in pairs(csvtitle) do
if var == findtitlename then
return key
end
end
return nil
end function csvtool:findLineByKeyAndValue(csvtable,key,value)
if type(csvtable) ~= "table" or table.getn(csvtable) == then
print("the argment 1 is not a csvtable or is a empty table")
return nil
end
if type(key) ~= "string" or key == "" then
print("the argment 2 is not a string or is a empty string")
return nil
end
if csvtable[][key] == nil then
print("the key is not found in csvtable")
return nil
end
local linereturn = {}
value = tostring(value)
local isFound = false
for k, v in pairs(csvtable) do
if v[key] == value then
isFound = true
table.insert(linereturn,v)
end
end
if isFound then
return linereturn
else
print("not found this key-value item")
return nil
end
end function csvtool:findoneLineByKeyAndValue(csvtable,key,value)
if type(csvtable) ~= "table" or table.getn(csvtable) == then
print("the argment 1 is not a csvtable or is a empty table")
return nil
end
if type(key) ~= "string" or key == "" then
print("the argment 2 is not a string or is a empty string")
return nil
end
if csvtable[][key] == nil then
print("the key is not found in csvtable")
return nil
end
local linereturn = {}
value = tostring(value)
for k, v in pairs(csvtable) do
if v[key] == value then
return v
end
end
print("not found this key-value item")
return nil
end function csvtool:findValueByOneKeyAndValue(csvtable,findkey,anotherkey,anothervalue) --find a value by another key-value
if type(csvtable) ~= "table" or table.getn(csvtable) == then
print("the argment 1 is not a csvtable or is a empty table")
return nil
end
if type(findkey) ~= "string" or findkey == "" then
print("the argment 2 is not a string or is a empty string")
return nil
end
if csvtable[][findkey] == nil then
print("the find key is not found in csvtable")
return nil
end
if csvtable[][anotherkey] == nil then
print("the another key is not found in csvtable")
return nil
end
local finditem = self:findoneLineByKeyAndValue(csvtable,anotherkey,anothervalue)
local foundvalue = finditem[findkey]
return foundvalue --string
end function csvtool:returnOneRowByKey(csvtable, rowkey)
if type(csvtable) ~= "table" or table.getn(csvtable) == then
print("the argment 1 is not a table or a empty table")
return nil
end
if type(rowkey) ~= "string" or rowkey == "" then
print("the argment 2 is not a string or is a empty string")
return nil
end
if csvtable[][rowkey] == nil then
print("the table is not have the rowkey row")
return nil
end
local returnRow = {}
local returnRowIndex =
for k, v in pairs(csvtable) do
returnRow[returnRowIndex] = v[rowkey]
returnRowIndex = returnRowIndex +
end
return returnRow
end function csvtool:getcsvsize(filename)
local csvtable = self:csv2table(filename)
local size = table.getn(csvtable)
return size
end function Split(szFullString, szSeparator)
local nFindStartIndex =
local nSplitIndex =
local nSplitArray = {}
while true do
local nFindLastIndex = string.find(szFullString, szSeparator, nFindStartIndex)
if not nFindLastIndex then
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, string.len(szFullString))
break
end
nSplitArray[nSplitIndex] = string.sub(szFullString, nFindStartIndex, nFindLastIndex - )
nFindStartIndex = nFindLastIndex + string.len(szSeparator)
nSplitIndex = nSplitIndex +
end
return nSplitArray
end

刚刚写的一个lua下解释csv的工具。的更多相关文章

  1. 开源一个IE下获取XPath小工具,支持32/64位

    背景是曾经友情支持了测试组一小段时间,发现他们使用selenium做页面的自动化测试,需要用到XPath,但IE下没有获取XPath的工具,只能在Firefox和chrome下获取,步骤还比较麻烦.而 ...

  2. rinetd 一个linux下的端口转发工具

    inux下使用iptables实现端口转发,配置较为复杂,使用rinetd工具可以实现快速配置和修改端口转发. 例:本机ip:1.1.1.1 需要实现访问本机的8080端口,自动转发到2.2.2.2 ...

  3. 自己写了一个解析json为table的工具类

    还需要完善的包括,css的封装,触发事件,数据的获得处理: <!DOCTYPE html> <html> <head> <meta charset=" ...

  4. cron Linux下的定时执行工具

    说明:测试平台  Ubuntu 16.04.4 LTS cron是一个Linux下的定时执行工具,可以在无需人工干预的情况下运行作业.所以,在Linux中,周期性执行的任务一般由cron这个守护进程来 ...

  5. linux 下的 crontab 定时任务工具

    以下内容转自:https://www.cnblogs.com/longjshz/p/5779215.html 在LINUX中,周期执行的任务一般由cron这个守护进程来处理[ps -ef|grep c ...

  6. lLinux 下 Stress 压力测试工具

    作者信息 邮箱:sijiayong000@163.com Q Q:601566386 Stress是什么 stress是一个linux下的压力测试工具,专门为那些想要测试自己的系统,完全高负荷和监督这 ...

  7. Tools - Windows系统下的命令行工具Cmder

    cmder简介 官网:http://cmder.net/ GitHub:https://github.com/cmderdev/cmder Cmder是一个windows下的命令行工具,用来替代win ...

  8. Cordova webapp实战开发:(6)如何写一个iOS下获取APP版本号的插件?

    上一篇我们学习了如何写一个Andorid下自动更新的插件,我想还有一部分看本系列blog的开发人员希望学习在iOS下如何做插件的吧,那么今天你就可以来看看这篇文字了. 本次练习你能学到的 学习如何获取 ...

  9. Cordova webapp实战开发:(5)如何写一个Andorid下自动更新的插件?

    在 <Cordova webapp实战开发:(4)Android环境搭建>中我们搭建好了开发环境,也给大家布置了调用插件的预习作业,做得如何了呢?今天我们来学一下如何自己从头建立一个And ...

随机推荐

  1. 说Gradle

      说Gradle 刚开始认识Gradle这个名词是在蘑菇街的一场 交流会上,当时只是一个概念:第二面,是试图下载编译spring源码的时候:第三面,就是我司较真的安卓主程,有一天兴高彩烈的跟我说,我 ...

  2. js常用代码收集

    1. PC - js 返回指定范围的随机数(m-n之间)的公式 Math.random()*(n-m)+m return false return false // event.preventDefa ...

  3. git学习--更新本地仓库单独文件

    命令: git checkout origin/dev .gitignore 从远端 dev 仓库,更新本地签出分支的 .gitignore 文件

  4. C#后端调用WebApi地址

    using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using Syst ...

  5. Educational Codeforces Round 53 (Rated for Div. 2) C. Vasya and Robot(二分或者尺取)

    题目哦 题意:给出一个序列,序列有四个字母组成,U:y+1,D:y-1 , L:x-1 , R:x+1;   这是规则 . 给出(x,y) 问可不可以经过最小的变化这个序列可以由(0,0) 变到(x, ...

  6. css雪碧图制作

    使用css背景合并工具cssSprite 工具下载链接: http://download.csdn.net/download/wx247919365/8741243 1.选择文件 2.生成雪碧图 3. ...

  7. Idea如何设置代码超出长度限制时自动换行

    在[File]-->[Settings]-->[Code Sytle]中勾选[Wrap on typing]选项

  8. python3 发送QQ邮件

    from email.header import Headerfrom email.mime.text import MIMETextfrom smtplib import SMTP_SSL emai ...

  9. Launch iCar Scan Android Scanner Support Bluetooth X431 iDiag Update Version

    Autonumen.com release new Launch iCar Scan for Android,Launch iCarScan Bluetooth Scanner is update v ...

  10. NETCore 调试

    https://www.cnblogs.com/MingQiu/p/8227644.html https://www.cnblogs.com/shumin/p/9967854.html 前言 core ...