你的位置:首页 > 操作系统

[操作系统]刚刚写的一个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[1],',')  local lineIndex = 1  local titleSize = table.getn(titleArray)  local returnTable = {}  for line = 2,table.getn(dataByLine)-1 do    local dataByLien_split = Split(dataByLine[line],',')    returnTable[lineIndex] = {}    for i = 1,titleSize do      returnTable[lineIndex][titleArray[i]] = dataByLien_split[i]    end    lineIndex = lineIndex+1  end  return returnTableendfunction csvtool:returnCsvTableTitle(csvtable)  local titleTable = {}  local titleIndex = 1  for key, var in pairs(csvtable[1]) do    titleTable[titleIndex] = key    titleIndex = titleIndex + 1  end  return titleTableendfunction csvtool:returnScvTitleIndex(csvtable,findtitlename)  if type(csvtable) ~= "table" or table.getn(csvtable) == 0 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 nilendfunction csvtool:findLineByKeyAndValue(csvtable,key,value)  if type(csvtable) ~= "table" or table.getn(csvtable) == 0 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[1][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  endendfunction csvtool:findoneLineByKeyAndValue(csvtable,key,value)  if type(csvtable) ~= "table" or table.getn(csvtable) == 0 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[1][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 nilendfunction csvtool:findValueByOneKeyAndValue(csvtable,findkey,anotherkey,anothervalue) --find a value by another key-value  if type(csvtable) ~= "table" or table.getn(csvtable) == 0 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[1][findkey] == nil then    print("the find key is not found in csvtable")    return nil  end  if csvtable[1][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 --stringendfunction csvtool:returnOneRowByKey(csvtable, rowkey)  if type(csvtable) ~= "table" or table.getn(csvtable) == 1 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[1][rowkey] == nil then    print("the table is not have the rowkey row")    return nil  end  local returnRow = {}  local returnRowIndex = 1  for k, v in pairs(csvtable) do    returnRow[returnRowIndex] = v[rowkey]    returnRowIndex = returnRowIndex + 1  end  return returnRowendfunction csvtool:getcsvsize(filename)  local csvtable = self:csv2table(filename)  local size = table.getn(csvtable)  return sizeendfunction Split(szFullString, szSeparator)   local nFindStartIndex = 1   local nSplitIndex = 1   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 - 1)   nFindStartIndex = nFindLastIndex + string.len(szSeparator)   nSplitIndex = nSplitIndex + 1   end   return nSplitArray end 

 




什么时候去陕西旅游最好去陕西旅游最佳路线陕西旅游景点推荐陕西跟团旅游报价多少陕西旅游线路大全澳大利亚圣诞岛旅游景点介绍 温州南麂列岛旅游景点介绍 山东梁山泊风景区_水泊梁山旅游景点介绍 江西三清山旅游景点介绍 佛山三水宝苞农场小孩免费吗?三水宝苞农场儿童票价格? 佛山宝苞农场几点开门?三水宝苞农场开门时间? 宝苞农场房间一晚多少钱?佛山三水宝苞农场住宿价格? 宝苞农场能住宿吗?佛山三水宝苞农场住宿多少钱? 指南村什么时候去最好?什么时候去临安指南村最好? 广州火车站到中山纪念堂怎么坐车?广州火车站到中山纪念堂乘车路线? 中山纪念堂附近美食介绍?广州中山纪念堂有吃饭的地方吗? 杭州到临安指南村怎么走? 迪拜使用什么货币?迪拜用什么钱的? 泰国曼谷的物价怎么样?比国内低吗? 泰国大皇宫对外开放吗?需要门票吗? 泰国金沙岛上有什么美食? 12062A1R0DAT2A Datasheet 12062A1R0DAT2A Datasheet SQCB7M620GA7WE Datasheet SQCB7M620GA7WE Datasheet AQ147A1R4BAJBE Datasheet AQ147A1R4BAJBE Datasheet 大兴安岭到港澳四天三晚游 大兴安岭到港澳四天三晚游 大兴安岭到港澳四天三晚游 浙江到港澳五天游 浙江到港澳五天游 浙江到港澳五天游 齐齐哈尔去香港2天旅游 齐齐哈尔去香港2天旅游 齐齐哈尔去香港2天旅游