将 lua 文件转换为 csv 或 tsv 或任何 excel 数据库文件

converting lua file to csv or tsv or any excel database file

我有一个 lua 文件,它是我在魔兽世界游戏中所有突袭的数据库。它由游戏中的插件生成,包含哪个玩家以哪个价格收到哪个战利品的信息。 像这样:

GDKPd_PotData = {
["playerBalance"] = {
},
["curPotHistory"] = {
},
["history"] = {
    {
        ["date"] = "Thu Apr 29 14:54:21 2021",
        ["note"] = false,
        ["items"] = {
            {
                ["bid"] = 225,
                ["item"] = "|cffa335ee|Hitem:19866::::::::60:::::::|h[Warblade of the Hakkari]|h|r",
                ["name"] = "Brighht",
            }, -- [1]
            {
                ["bid"] = 50,
                ["item"] = "|cff0070dd|Hitem:19907::::::::60:::::::|h[Zulian Tigerhide Cloak]|h|r",
                ["name"] = "Treasuredon",
            }, -- [2]
            {
                ["bid"] = 50,
                ["item"] = "|cffa335ee|Hitem:19897::::::::60:::::::|h[Betrayer's Boots]|h|r",
                ["name"] = "Sassysis",
            }, -- [3]
            {
                ["bid"] = 50,
                ["item"] = "|cff0070dd|Hitem:19895::::::::60:::::::|h[Bloodtinged Kilt]|h|r",
                ["name"] = "Yabmage",
            }, -- [4]
            {
                ["bid"] = 180,
                ["item"] = "|cffa335ee|Hitem:19856::::::::60:::::::|h[The Eye of Hakkar]|h|r",
                ["name"] = "Consti",
            }, -- [5]
            {
                ["bid"] = 100,
                ["item"] = "|cffa335ee|Hitem:19802::::::::60:::::::|h[Heart of Hakkar]|h|r",
                ["name"] = "Consti",
            }, -- [6]
            {
                ["bid"] = 300,
                ["item"] = "|cff0070dd|Hitem:22637::::::::60:::::::|h[Primal Hakkari Idol]|h|r",
                ["name"] = "Gnomepowah",
            }, -- [7]
            {
                ["bid"] = 220,
                ["item"] = "|cff0070dd|Hitem:22637::::::::60:::::::|h[Primal Hakkari Idol]|h|r",
                ["name"] = "Lockbik",
            }, -- [8]
            {
                ["bid"] = 110,
                ["item"] = "|cffa335ee|Hitem:20725::::::::60:::::::|h[Nexus Crystal]|h|r",
                ["name"] = "Aeto",
            }, -- [9]
        },
        ["size"] = 1285,
    }, -- [1]

我的想法是将其转换为 excel,我可以在其中创建统计数据和图形并管理信息。

我浏览了整个网络,找不到任何程序可以将 lua 转换成任何 excel 文件格式(xls、csv、tsv)

谁能给我一些关于如何最好地做到这一点的想法?

CSV 和 TSV 基本上是一样的,只是分隔符不同。

写 .xls 或 xlsx 有点复杂。

如果您想使用 xlsx,请打开网络浏览器并在任何网络搜索中输入“lua xlsx”。例如,您会发现 https://github.com/jmcnamara/xlsxwriter.lua

--
-- A simple example of some of the features of the xlsxwriter module.
--

local Workbook = require "xlsxwriter.workbook"

local workbook  = Workbook:new("demo.xlsx")
local worksheet = workbook:add_worksheet()

-- Widen the first column to make the text clearer.
worksheet:set_column("A:A", 20)

-- Add a bold format to use to highlight cells.
local bold = workbook:add_format({bold = true})

-- Write some simple text.
worksheet:write("A1", "Hello")

-- Text with formatting.
worksheet:write("A2", "World", bold)

-- Write some numbers, with row/column notation.
worksheet:write(2, 0, 123)
worksheet:write(3, 0, 123.456)

workbook:close()

编写 CSV 或 TSV 很简单。您只需要使用 Lua's io library 将值写入文件并用逗号或制表符分隔。

这两种解决方案都需要您使用一些 for 循环递归遍历 table。