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

问题描述 投票:0回答:1

我玩了一款名为DCS(数字战斗模拟器)的游戏,当时希望将游戏统计信息导出到数据库中,然后在网页上使用PHP进行调用。游戏将代码保存到LUA表中,示例数据如下所示...

类别显示飞机的类型,飞行时间,被杀死的飞机,飞机的类型,被杀死的飞机总数,武器,武器类型,命中率,杀伤力,射击,动作,损失,飞行员死亡,坠毁,弹射,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表解析为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关联的类别,在看到“-stat stats”时使其结束解析,并推送到数据库以最终显示网页上的PHP?

[问了很多,我很抱歉,但是我已经研究了一段时间,但收效甚微。认为我有一个不错的基础。

python sql lua mariadb lua-table
1个回答
0
投票

仅使用Lua解析文件怎么样?

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

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

© www.soinside.com 2019 - 2024. All rights reserved.