如何高效从Lua文件中提取并处理大量数据?
- 内容介绍
- 文章标签
- 相关推荐
本文共计227个文字,预计阅读时间需要1分钟。
我想知道有没有办法从文件读取数据,或者只是看看它是否存在并返回true或false。函数定义如下:`function fileRead(Path, LineNumber) --..Code... return Dataend` 尝试这个:+--+http://lua-users.org/wiki/FileInputOutput--+看看是否有效。
我想知道有没有办法从文件读取数据,或者只是看看它是否存在并返回true或falsefunction fileRead(Path,LineNumber) --..Code... return Data end 尝试这个:
-- lua-users.org/wiki/FileInputOutput -- see if the file exists function file_exists(file) local f = io.open(file, "rb") if f then f:close() end return f ~= nil end -- get all lines from a file, returns an empty -- list/table if the file does not exist function lines_from(file) if not file_exists(file) then return {} end lines = {} for line in io.lines(file) do lines[#lines + 1] = line end return lines end -- tests the functions above local file = 'test.lua' local lines = lines_from(file) -- print all line numbers and their contents for k,v in pairs(lines) do print('line[' .. k .. ']', v) end
本文共计227个文字,预计阅读时间需要1分钟。
我想知道有没有办法从文件读取数据,或者只是看看它是否存在并返回true或false。函数定义如下:`function fileRead(Path, LineNumber) --..Code... return Dataend` 尝试这个:+--+http://lua-users.org/wiki/FileInputOutput--+看看是否有效。
我想知道有没有办法从文件读取数据,或者只是看看它是否存在并返回true或falsefunction fileRead(Path,LineNumber) --..Code... return Data end 尝试这个:
-- lua-users.org/wiki/FileInputOutput -- see if the file exists function file_exists(file) local f = io.open(file, "rb") if f then f:close() end return f ~= nil end -- get all lines from a file, returns an empty -- list/table if the file does not exist function lines_from(file) if not file_exists(file) then return {} end lines = {} for line in io.lines(file) do lines[#lines + 1] = line end return lines end -- tests the functions above local file = 'test.lua' local lines = lines_from(file) -- print all line numbers and their contents for k,v in pairs(lines) do print('line[' .. k .. ']', v) end

