简体   繁体   English

从 LUA 中的文本文件生成列表/数组

[英]generating list/array from a text file in LUA

I'm trying to handle a text file containing a list of numbers (generated from a python script) into a LUA script.我正在尝试将包含数字列表(从 python 脚本生成)的文本文件处理为 LUA 脚本。

the text file contain this (I formatted it in the python script to match the list syntax of LUA) :文本文件包含这个(我在 python 脚本中格式化它以匹配 LUA 的列表语法):

{"0.0", "0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7", "0.8", "0.9", "1.0", "834.3", "1667.6", "2500.9", "3334.2", "4167.5", "5000.8", "5834.1", "6667.4", "7500.7", "8334.0", "9167.3", "10000.6", "10833.9", "11667.2", "12500.5", "13333.8", "14167.1", "15000.4", "15833.7", "16667.0", "17500.3", "18333.6", "19166.9", "20000.2", "20833.5", "21666.8", "22500.1", "23333.4", "24166.7", "25000.0", "22244444.44", "44463888.89", "66683333.33", "88902777.78", "111122222.22", "133341666.67", "155561111.11", "177780555.56", "200000000.0", }

However when I try to read it in LUA, first I use this command (where "params.style_weight_list" is a link to the text file previously generated) :但是,当我尝试在 LUA 中读取它时,首先我使用此命令(其中“params.style_weight_list”是指向先前生成的文本文件的链接):

print("SW LIST = ",params.style_weight_list)

which gives me this result :这给了我这个结果:

SW LIST =   {"0.0", "0.1", "0.2", "0.3", "0.4", "0.5", "0.6", "0.7", "0.8", "0.9", "1.0", "834.3", "1667.6", "2500.9", "3334.2", "4167.5", "5000.8", "5834.1", "6667.4", "7500.7", "8334.0", "9167.3", "10000.6", "10833.9", "11667.2", "12500.5", "13333.8", "14167.1", "15000.4", "15833.7", "16667.0", "17500.3", "18333.6", "19166.9", "20000.2", "20833.5", "21666.8", "22500.1", "23333.4", "24166.7", "25000.0", "22244444.44", "44463888.89", "66683333.33", "88902777.78", "111122222.22", "133341666.67", "155561111.11", "177780555.56", "200000000.0", }    

yet everything seems good (there is several blank spaces before the first bracket, don't know why though)然而一切似乎都很好(第一个括号前有几个空格,但不知道为什么)

but when I try to access a specific item from the list this way (frameIdx is an int) :但是当我尝试以这种方式访问​​列表中的特定项目时(frameIdx 是一个 int):

local neu_sw = params.style_weight_list[frameIdx]

it return a nil value, so I think I got to convert the imported list from a string to a proper usable list in LUA but don't know how ?它返回一个 nil 值,所以我想我必须将导入的列表从字符串转换为 LUA 中正确可用的列表,但不知道如何? anybody has an idea ?有人有想法吗?

When you're unsure what's happening in the script you can always debug the code.当您不确定脚本中发生了什么时,您可以随时调试代码。

  1. Use print and type to see what value you have and what its type is.使用printtype来查看您拥有的值及其类型。

  2. Inspect your table with for k,v in pairs(tbl) do print(k,v) end to see if it really has that key.检查你的表for k,v in pairs(tbl) do print(k,v) end看看它是否真的有那个键。

As I can see, params.style_weight_list is not a table but a string.如我所见, params.style_weight_list不是表格而是字符串。 You can use load to convert string into script with a Lua table, loadfile to read it from a file directly (or with require , this requires you to prepend return at the beginning of the file), or make a parser that reads values (more proper way but complicated for beginners)您可以使用load将字符串转换为带有 Lua 表的脚本,使用loadfile直接从文件中读取它(或使用require ,这需要您在文件开头添加return ),或者制作一个读取值的解析器(更多正确的方法,但对初学者来说很复杂)

Thanks for your help Nifim and Spar !感谢您的帮助 Nifim 和 Spar!

thanks to your insight I got it working.感谢你的洞察力,我让它工作了。 if this can help, here what I did :如果这可以帮助,这里我做了什么:

  • export my python array into a txt file ( with a lua extension, probably useless but helped me keeping it clear in my folder) with one value by line and no other formating elements.将我的 python 数组导出到一个 txt 文件(带有 lua 扩展名,可能没用,但帮助我在​​我的文件夹中保持清晰),逐行一个值,没有其他格式元素。 "a" is my array in python. “a”是我在python中的数组。

    LUA_in = n*'{}\\n' LUA_in = n*'{}\\n'
    LUA_format = LUA_in.format(*a) LUA_format = LUA_in.format(*a)
    LUA_file = open("sw.lua","w") LUA_file = open("sw.lua","w")
    LUA_file.write(LUA_format) LUA_file.write(LUA_format)

  • and then read it in lua, create an empty table, add one value/line per table lines and read the specific line I need.然后在 lua 中读取它,创建一个空表,为每个表行添加一个值/行并读取我需要的特定行。

    sw_array = {} sw_array = {}
    for line in io.lines("sw.lua") do对于 io.lines("sw.lua") 中的行做
    table.insert(sw_array, line) table.insert(sw_array,行)
    end结尾

    local cur_sw = sw_array[frameIdx+1]本地 cur_sw = sw_array[frameIdx+1]

where frameIdx is the specific value Index I want to read (and "+1" because lua tables start counting from 1 and python from 0)其中 frameIdx 是我想要读取的特定值索引(和“+1”,因为 lua 表从 1 开始计数,python 从 0 开始计数)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM