刚刚写的一个lua下解释csv的工具。
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的工具。的更多相关文章
- 开源一个IE下获取XPath小工具,支持32/64位
背景是曾经友情支持了测试组一小段时间,发现他们使用selenium做页面的自动化测试,需要用到XPath,但IE下没有获取XPath的工具,只能在Firefox和chrome下获取,步骤还比较麻烦.而 ...
- rinetd 一个linux下的端口转发工具
inux下使用iptables实现端口转发,配置较为复杂,使用rinetd工具可以实现快速配置和修改端口转发. 例:本机ip:1.1.1.1 需要实现访问本机的8080端口,自动转发到2.2.2.2 ...
- 自己写了一个解析json为table的工具类
还需要完善的包括,css的封装,触发事件,数据的获得处理: <!DOCTYPE html> <html> <head> <meta charset=" ...
- cron Linux下的定时执行工具
说明:测试平台 Ubuntu 16.04.4 LTS cron是一个Linux下的定时执行工具,可以在无需人工干预的情况下运行作业.所以,在Linux中,周期性执行的任务一般由cron这个守护进程来 ...
- linux 下的 crontab 定时任务工具
以下内容转自:https://www.cnblogs.com/longjshz/p/5779215.html 在LINUX中,周期执行的任务一般由cron这个守护进程来处理[ps -ef|grep c ...
- lLinux 下 Stress 压力测试工具
作者信息 邮箱:sijiayong000@163.com Q Q:601566386 Stress是什么 stress是一个linux下的压力测试工具,专门为那些想要测试自己的系统,完全高负荷和监督这 ...
- Tools - Windows系统下的命令行工具Cmder
cmder简介 官网:http://cmder.net/ GitHub:https://github.com/cmderdev/cmder Cmder是一个windows下的命令行工具,用来替代win ...
- Cordova webapp实战开发:(6)如何写一个iOS下获取APP版本号的插件?
上一篇我们学习了如何写一个Andorid下自动更新的插件,我想还有一部分看本系列blog的开发人员希望学习在iOS下如何做插件的吧,那么今天你就可以来看看这篇文字了. 本次练习你能学到的 学习如何获取 ...
- Cordova webapp实战开发:(5)如何写一个Andorid下自动更新的插件?
在 <Cordova webapp实战开发:(4)Android环境搭建>中我们搭建好了开发环境,也给大家布置了调用插件的预习作业,做得如何了呢?今天我们来学一下如何自己从头建立一个And ...
随机推荐
- Color Length UVA - 1625
题目大意: 给你两个字符串p,q,字符串中每个字符代表一个颜色,现在按顺序合并两个字符串,得到一个新字符串.新字符串的价值为,每个颜色价值的和,单个颜色价值的和等于该颜色在新字符中最后一次出现的位置减 ...
- 微信小程序传数组(Json字符串)到Java后端
一:小程序端: wxml中代码: <!--index.wxml--> <view> <view> <button bindtap="onShow&q ...
- POJ 1000 A+B
#include <stdio.h> int main() { int a,b; scanf("%d %d",&a, &b); printf(" ...
- windows_study_3
描述:如何解决hyper-v全屏不能自适应屏幕大小? 解决:调节虚拟机的分辨率.
- 江西财经大学第一届程序设计竞赛 C
链接:https://www.nowcoder.com/acm/contest/115/C来源:牛客网 题目描述 决赛圈还剩下两个人,“伏地魔”XDD和跑毒进圈的FZL,XDD拿着狙击枪AWM瞄准并准 ...
- 毕业设计 python opencv实现车牌识别 颜色定位
主要代码参考https://blog.csdn.net/wzh191920/article/details/79589506 GitHub:https://github.com/yinghualuow ...
- log4j2重复打印日志问题解决
log4j2.xml <?xml version="1.0" encoding="UTF-8"?> <Configuration> &l ...
- etcd介绍
etcd是一个开源的.分布式的键值对数据存储系统,提供共享配置.服务的注册和发现. etcd与zookeeper相比算是轻量级系统.etcd的raft比zookeeper的paxos简单. 我们用et ...
- java——删除链表中等于给定值的所有元素
class ListNode{ int val ; ListNode next; public ListNode(int x) { val = x; } public ListNode(int[] a ...
- java——斗地主小游戏之洗牌发牌
遇到的问题: 1.int和Integer的区别? 1)Integer是int的包装类,int则是java的一种基本数据类型 . 2)Integer变量必须实例化后才能使用,而int变量不需要 . 3) ...