lua打开一个包含表格的文件并更改特定值

我有一个包含大量表格的文件。需要打开它,获取某些值并用新值替换其他值。在这个例子中,Females和Males Classroom a和b必须等于Teachers Classroom a和b。 阅读了许多有关IO的内容,但我似乎无法正确地使其工作。任何帮助将不胜感激。

--用于测试的示例数据文件,原始文件有7000多行
    theData =
    {
        ["theSchool"] =
            {
            ["theTeachers"] =
            {
                ["theClassroom"] =
                {
                    ["a"] = 001,
                    ["b"] = 002,
                    },
                },
                ["theFemales"] =
                {
                    ["theClassroom"] =
                    {
                        ["a"] = 005,
                        ["b"] = 010,
                    },
                },
                ["theMales"] =
                {
                    ["theClassroom"] =
                    {
                        ["a"] = 012,
                        ["b"] = 014,
                    },
                },
            },
    }

然后文件函数就像这样

local file = io.open(filepath,'r')
local newCa = '["a"] = '..theData.theSchool.theTeachers.theClassroom.a
local newCb = '["b"] = '..theData.theSchool.theTeachers.theClassroom.b
local replaceFa = '["a"] = '..tostring(theData.theSchool.theFemales.theClassroom.a)
local replaceFb = '["b"] = '..tostring(theData.theSchool.theFemales.theClassroom.b)
local replaceMa = '["a"] = '..tostring(theData.theSchool.theMales.theClassroom.a)
local replaceMb = '["b"] = '..tostring(theData.theSchool.theMales.theClassroom.b)
file:close()
--它会工作吗?
print("原始值:")
print(newCa)
print(newCb)
print(replaceFa)
print(replaceFb)
print(replaceMa)
print(replaceMb)
file = io.open(filepath,'r+')
function lines_from(filepath)
  lines = {}
  for line in io.lines(filepath) do
    lines[#lines + 1] = line
  end
  return lines
end
local lines = lines_from(filepath)
for k,v in ipairs(lines) do
  if v == replaceFa then
    file:write(newCa..'\n')
  elseif v == replaceFb then
    file:write(newCb..'\n')
  elseif v == replaceMa then
    file:write(newCa..'\n')
  elseif v == replaceMb then
    file:write(newCb..'\n')
  else
    file:write(v..'\n')
  end
  --('[' .. k .. ']', v)
end
--replaceF = {[a] = newCa, [b] = newCb}
--replaceM = {[a] = newCa, [b] = newCb}
--file:write(replaceF)
--file:write(replaceM)
--print(tostring(theData.theSchool.theFemales.theClassroom.a))
file:close()
file = io.open(filepath,'r')
--它是否起作用?
print("新值:")
print(theData.theSchool.theTeachers.theClassroom.a)
print(theData.theSchool.theTeachers.theClassroom.b)
print(theData.theSchool.theFemales.theClassroom.a)
print(theData.theSchool.theFemales.theClassroom.b)
print(theData.theSchool.theMales.theClassroom.a)
print(theData.theSchool.theMales.theClassroom.b)```

> 在它起作用后,我会删除所有打印信息。
点赞
用户7504558
用户7504558

尝试这个方案,一次性读取文件中的表格将更快,并且一次性写入也更好用。

--- 读取
local handle = io.open("data.lua",'rb')
local data  = handle:read("*a")
handle:close()

data = data .. "\n return theData"

local t = loadstring(data)()

-- 编辑
theData.theSchool.theTeachers.theClassroom.a = theData.theSchool.theTeachers.theClassroom.a + 1

-- 写入
local function concat(t, r)
     r = r or {}
     for k,v in pairs(t)  do
         if type(v)=="table" then
            r[#r+1] = string.format('\t["%s"]={\n',k  )
            concat(v, r)
            r[#r+1] = "\t},\n"
         else
            r[#r+1] = string.format('\t\t["%s"]=%03s,\n',k ,v )
         end
     end
     return r
end
local r = concat(t)
local text = "theData = { \n " .. table.concat(r) .. "}"
print(text) -- 只是控制

local handle = io.open("data.lua",'wb')
local data  = handle:write(text)
handle:close()
2020-10-08 17:40:23