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

w1jd8yoj  于 2023-07-31  发布在  其他
关注(0)|答案(1)|浏览(133)

我有一个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)。
谁能给予我一些建议,如何最好地做到这一点?

camsedfj

camsedfj1#

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的io库将值写入文件,并使用逗号或制表符将它们分隔开。
这两种解决方案都需要使用几个for循环递归地遍历表。

相关问题