首页 > 代码库 > Lua读取CSV文件到table中

Lua读取CSV文件到table中

创建Lua函数载入CSV文件并保存到表中的函数:

function GetLines(fileName)	indx = 0	myLines ={} 	for line in io.line(string.format("%s%s", "c:/lua_scripts/",filename)) do		indx = indx + 1		myLines[indx] = line	end	return indx, myLines --returns number of lines and line tableendfunction GetValues(myString)	num = 0	values = {}	if myString ~= nil then		while string.find(myString, ",") ~= nil do			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, valuesend