Lua - 将表格转换为逗号分隔列表

我需要将一个表格转换成逗号分隔的列表,以便将其保存到文本文件中。Lua 里有内置的方法可以实现这个吗?

原文链接 https://stackoverflow.com/questions/6589617

点赞
stackoverflow用户63791
stackoverflow用户63791

没有内置的方法,但是如果你想自己构建,有许多选项是相对容易的。以下是一些链接,可以帮助你决定如何组合它:

http://www.lua.org/pil/12.1.html

http://lua-users.org/wiki/TableSerialization

2011-07-05 22:39:03
stackoverflow用户734069
stackoverflow用户734069

不,没有一个“内置”功能可以做到这一点。但自己做也不难。我保留了一个脚本,可以递归地将Lua表直接写入Lua脚本文件,然后可以像Lua脚本一样加载和执行。

--此文件导出一个功能WriteTable,它将给定表写入给定的文件句柄。

local writeKey = {};

function writeKey.string(hFile, value, iRecursion)
    WriteFormatted(hFile, "[\"%s\"]", value);
end

function writeKey.number(hFile, value, iRecursion)
    WriteFormatted(hFile, "[%i]", value);
end

local writeValue = {};

function writeValue.string(hFile, value, iRecursion)
    WriteFormatted(hFile, "[==[%s]==]", value);
end

function writeValue.number(hFile, value, iRecursion)
    WriteFormatted(hFile, "%i", value);
end

function writeValue.boolean(hFile, value, iRecursion)
    if(value) then hFile:write("true"); else hFile:write("false"); end;
end

function writeValue.table(hFile, value, iRecursion)
    WriteTable(hFile, value, iRecursion)
end

local function WriteFormatted(hFile, strFormat, ...)
    hFile:write(string.format(strFormat, ...));
end

local function WriteForm(hFile, strFormat, ...)
    hFile:write(string.format(strFormat, ...));
end

local function WriteTabs(hFile, iRecursion)
    for iCount = 1, iRecursion, 1 do
        hFile:write("\t");
    end
end

function WriteTable(hFile, outTable, iRecursion)
    if(iRecursion == nil) then iRecursion = 1; end

    hFile:write("{\n");

    local bHasArray = false;
    local arraySize = 0;

    if(#outTable > 0) then bHasArray = true; arraySize = #outTable; end;

    for key, value in pairs(outTable) do
        if(writeKey[type(key)] == nil) then print("Malformed table key."); return; end
        if(writeValue[type(value)] == nil) then
            print( string.format("Bad value in table: key: '%s' value type '%s'.", key, type(value)));
            return;
        end

        --如果键不是数组索引,请处理它。
        if((not bHasArray) or
                (type(key) ~= "number") or
                not((1 <= key) and (key <= arraySize))) then
            WriteTabs(hFile, iRecursion);
            writeKey[type(key)](hFile, key, iRecursion + 1);
            hFile:write(" = ");
            writeValue[type(value)](hFile, value, iRecursion + 1);

            hFile:write(",\n");
        end
    end

    if(bHasArray) then
        for i, value in ipairs(outTable) do
            WriteTabs(hFile, iRecursion);
            writeValue[type(value)](hFile, value, iRecursion + 1);
            hFile:write(",\n");
        end
    end

    WriteTabs(hFile, iRecursion - 1);
    hFile:write("}");
end
2011-07-05 22:40:50
stackoverflow用户37843
stackoverflow用户37843

没有内置的函数,但是网上有示例。

这个示例其实还不错。

2011-07-05 22:43:04
stackoverflow用户107090
stackoverflow用户107090

如果你的表是一个数组,可以使用 table.concat 来打印 CSV:

t={10,20,30}
print(table.concat(t,","))

输出 10,20,30

2011-07-06 00:00:19
stackoverflow用户6038447
stackoverflow用户6038447

是的,Lua 有一个内置方法 table.concat,而且它已经存在很长时间了。

-- table.concat
local line = { "Fred", 20, 4.000 }
print(table.concat(line,","))

输出: Fred, 20, 4.000

使用这个函数可以将一个表格转换成字符串,然后可以选择一个逗号","作为分隔符。您还可以添加一个函数,在连接过程中检测您写入了多少属性,然后添加一个新行 - 如果需要,您可以创建一个非常复杂的转换器。

我的建议是将每个逗号分隔的表格“行”分开,并使用逗号将每个表格连接起来,然后写出来。这样,您可以确保可以处理大量的行,并且每行都格式正确。

注意:

  • 您必须处理带有逗号、引号等的字符串。
  • 这种方法主要用于有序表(列表或数组)。它们必须被索引。
  • 如果需要对表格中的值进行处理,请先处理,然后再连接。

concat 参考: http://www.lua.org/manual/5.1/manual.html#pdf-table.concat

2020-09-09 04:32:39