如何解析 LUA Table 并使用 Python 推送到 SQL 数据库?

How to parse a LUA Table and push to SQL Database using Python?

我玩了一款名为 DCS(数字战斗模拟器)的游戏,希望将游戏统计数据导出到数据库,然后在网页上使用 PHP 回忆。游戏保存代码到LUATable,示例数据如下所示...

类别显示飞机类型、飞行时间、被杀飞机、飞机类型、被杀飞机总数、武器、武器类型、命中、击杀、射击、动作、损失、飞行员死亡、坠毁、弹射、pvp、击杀、加入日期、上次加入、姓名等等。

stats = 
{
    ["b3961df7f720c4288522019d0455fa4a"] = 
    {
        ["times"] = 
        {
            ["AV8BNA"] = 
            {
                ["inAir"] = 1441.802,
                ["weapons"] = 
                {
                    ["AIM-9M"] = 
                    {
                        ["numHits"] = 0,
                        ["kills"] = 0,
                        ["shot"] = 1,
                        ["hit"] = 0,
                    }, -- end of ["AIM-9M"]
                    ["kamikaze"] = 
                    {
                        ["numHits"] = 0,
                        ["kills"] = 0,
                        ["shot"] = 0,
                        ["hit"] = 0,
                    }, -- end of ["kamikaze"]
                    ["AGM-122"] = 
                    {
                        ["numHits"] = 0,
                        ["kills"] = 0,
                        ["shot"] = 1,
                        ["hit"] = 0,
                    }, -- end of ["AGM-122"]
                }, -- end of ["weapons"]
                ["actions"] = 
                {
                    ["losses"] = 
                    {
                        ["pilotDeath"] = 1,
                        ["crash"] = 1,
                        ["eject"] = 0,
                    }, -- end of ["losses"]
                }, -- end of ["actions"]
                ["total"] = 2132.704,
            }, -- end of ["AV8BNA"]
            ["FA-18C_hornet"] = 
            {
                ["total"] = 13412.988,
                ["kills"] = 
                {
                    ["Planes"] = 
                    {
                        ["Fighters"] = 6,
                        ["total"] = 6,
                    }, -- end of ["Planes"]
                }, -- end of ["kills"]
                ["inAir"] = 8568.488,
                ["weapons"] = 
                {
                    ["M-61"] = 
                    {
                        ["hit"] = 0,
                        ["kills"] = 0,
                        ["shot"] = 664,
                        ["numHits"] = 48,
                    }, -- end of ["M-61"]
                    ["AGM-88C"] = 
                    {
                        ["hit"] = 0,
                        ["kills"] = 0,
                        ["shot"] = 1,
                        ["numHits"] = 0,
                    }, -- end of ["AGM-88C"]
                    ["AIM-120C"] = 
                    {
                        ["hit"] = 0,
                        ["kills"] = 4,
                        ["shot"] = 4,
                        ["numHits"] = 4,
                    }, -- end of ["AIM-120C"]
                    ["F/A-18C Lot 20"] = 
                    {
                        ["numHits"] = 0,
                        ["kills"] = 0,
                        ["shot"] = 0,
                        ["hit"] = 0,
                    }, -- end of ["F/A-18C Lot 20"]
                    ["AIM-9X"] = 
                    {
                        ["hit"] = 0,
                        ["kills"] = 2,
                        ["shot"] = 6,
                        ["numHits"] = 4,
                    }, -- end of ["AIM-9X"]
                    ["AGM-154A"] = 
                    {
                        ["hit"] = 0,
                        ["kills"] = 0,
                        ["shot"] = 2,
                        ["numHits"] = 0,
                    }, -- end of ["AGM-154A"]
                }, -- end of ["weapons"]
                ["actions"] = 
                {
                    ["losses"] = 
                    {
                        ["pilotDeath"] = 4,
                        ["crash"] = 4,
                        ["eject"] = 0,
                    }, -- end of ["losses"]
                }, -- end of ["actions"]
                ["pvp"] = 
                {
                    ["kills"] = 6,
                }, -- end of ["pvp"]
            }, -- end of ["FA-18C_hornet"]
            ["F-5E-3"] = 
            {
                ["inAir"] = 0,
                ["total"] = 3574.806,
            }, -- end of ["F-5E-3"]
            ["Ka-50"] = 
            {
                ["inAir"] = 0,
                ["total"] = 3034.86,
            }, -- end of ["Ka-50"]
            ["F-15C"] = 
            {
                ["inAir"] = 700.745,
                ["actions"] = 
                {
                    ["losses"] = 
                    {
                        ["pilotDeath"] = 1,
                        ["crash"] = 1,
                        ["eject"] = 0,
                    }, -- end of ["losses"]
                }, -- end of ["actions"]
                ["total"] = 5986.455,
            }, -- end of ["F-15C"]
            ["UH-1H"] = 
            {
                ["inAir"] = 0,
                ["total"] = 420.383,
            }, -- end of ["UH-1H"]
            ["F-14B"] = 
            {
                ["total"] = 21862.031,
                ["kills"] = 
                {
                    ["Planes"] = 
                    {
                        ["Fighters"] = 4,
                        ["total"] = 4,
                    }, -- end of ["Planes"]
                }, -- end of ["kills"]
                ["inAir"] = 11952.818,
                ["weapons"] = 
                {
                    ["AIM_54A_Mk60"] = 
                    {
                        ["hit"] = 0,
                        ["kills"] = 4,
                        ["shot"] = 4,
                        ["numHits"] = 5,
                    }, -- end of ["AIM_54A_Mk60"]
                }, -- end of ["weapons"]
                ["actions"] = 
                {
                    ["losses"] = 
                    {
                        ["pilotDeath"] = 0,
                        ["crash"] = 3,
                        ["eject"] = 0,
                    }, -- end of ["losses"]
                }, -- end of ["actions"]
                ["pvp"] = 
                {
                    ["kills"] = 4,
                }, -- end of ["pvp"]
            }, -- end of ["F-14B"]
        }, -- end of ["times"]
        ["joinDate"] = 1589674831,
        ["lastJoin"] = 1591926810,
        ["id"] = 3,
        ["names"] = 
        {
            [1] = "DRAGON 1-2 | DeathTrooper",
            [2] = "DeathTrooper",
        }, -- end of ["names"]
} -- end of stats

我正在寻求有关如何分离样本数据中的值的建议(一种通过 Python 将 LUA Table 解析为 SQL 的方法)以及将它们全部绑定到 UUID(唯一用户标识),如上面的字符串 "b3961df7f720c4288522019d0455fa4a" 所示。

我目前用来加载所有这些数据的 Python 代码如下...

import mysql.connector

# Make connection to DB with below...
db = mysql.connector.connect(
    host="192.168.1.20",
    user="user",
    passwd="password",
    db="database"
)
# Declare cursor
cursor = db.cursor()
# Open file to be read and imported to table in "database" database
file = open(r"C:\Users\Username\Desktop\slmodstats.lua", "r", encoding='utf-8')
file_content = file.read()
file.close()

# Debug for me to see what the file content is...
print(file_content, "\n")

# Send data to table VALUES type LONGTEXT as one long string (proof of concept)
# Push all that was read and is now "%s" from prior string and set query...
query = "REPLACE INTO data VALUES (%s);"
# insert all data from "query" and file content as %s, push to db
cursor.execute(query, (file_content,))
# Commit changes and close...
db.commit()
db.close()

有什么方法可以更轻松地解析 LUA Table 文件中的数据,将其过滤到与 UUID 绑定的类别中,使其在看到“-- end of stats 时结束解析” ",并推送到数据库以最终在网页上显示 PHP?

问了很多,很抱歉,但我研究了一段时间,现在收效甚微。想想我有一个不错的基础。

只使用 Lua 来解析文件怎么样?

#!/usr/bin/env lua
dofile("stat.data")
for k,v in pairs(stats) do
    -- generate a data format that can be easily parsed by your program
    -- e.g. json
end

然后你可以使用管道或临时文件来连接这两个程序