首页 > 解决方案 > 如何使用 lua 在 python 中创建列表和附加值

问题描述

我正在尝试使用本地目录中的文件名创建一个列表,并使用 lua 将其添加到 nginx 中的 http 请求正文。像下面的东西

filelist = [] #a list variable to hold the filenames
for file in f:lines() do
   #file i get my filename one by one
   filelist.append(file)
end

request_payload = {"somedata":"its key", "files": filelist}

假设我有请求正文,我需要用新的 filelist 变量值更新那个 json。

我知道 lua 没有像列表对象这样的东西,而是它具有表结构。但是有什么简单的技巧可以完成这项工作

标签: nginxluaopenresty

解决方案


我让它与 lua 数组一起使用

local filelist = {} #lua table object
for file in f:lines() do
   #file i get my filename one by one
   table.insert(filelist, file) #this will update the array
end

希望对某人有所帮助:)


推荐阅读