如何使用python解析lua表并推送到sql数据库?

efzxgjgh  于 2021-08-09  发布在  Java
关注(0)|答案(1)|浏览(428)

我玩了一个名为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表文件中的数据,将其过滤到与uuid相关的类别中,在看到“-end of stats”时使其结束解析,并将其推送到数据库中,最终用php显示在网页上?
问了很多,我很抱歉,但我已经研究了一段时间,没有什么结果。不过我想我有一个不错的基金会。

knsnq2tg

knsnq2tg1#

用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

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

相关问题