functionGetLines(fileName) index = 0 myLines = {} for line inio.lines(string.format("%s%s", "./", fileName)) do index = index + 1 myLines[index] = line end return index, myLines --返回文件的行数和一个包括所有行的表 end
functionGetValues(myString) num = 0 values = {} if myString ~= nilthen whilestring.find(myString,",") ~= nildo i,j = string.find(myString,",") num = num + 1 values[num] = string.sub(myString,1, j-1) myString = string.sub(myString, j+1, string.len(myString)) end num = num + 1--接着对第二行数据 values[num] = myString end return num, values end
functionLoadDatas() myCharacters = {} numLines, allLines = GetLines("data.csv") --读第一行的键值 count, myLabels = GetValues(allLines[1]) --忽略第一行 for index = 2, numLines do count, charHold = GetValues(allLines[index]) myCharacters[index-1] = {} for index2 = 1, count do myCharacters[index-1][index2] = charHold[index2] end end --now print them for index = 1, 3do for index2 = 1, table.getn(myLabels) do print(myLabels[index2], myCharacters[index][index2]) end end end