簡體   English   中英

將來自 json output 的特定值列表存儲到 Python 中的文本文件中

[英]Store a list of specific values from json output into a text file in Python

我正在使用翼手龍來托管我的 minecraft 服務器,並且我一直在使用 API 制作工具來控制它,因此我不必在托管它的機器上使用 go 或登錄到 minecraft。 我希望能夠從 json output api 中獲取服務器名稱及其各自的 UUID 並將它們存儲在文件中,以便我可以在需要指定某項操作時指定服務器。 有沒有辦法可以制作這樣的過濾列表並在我想要執行操作時引用它?

以下是 output 的示例:

{
   "object":"list",
   "data":[
      {
         "object":"server",
         "attributes":{
            "server_owner":False,
            "identifier":"f7c8518a",
            "internal_id":1,
            "uuid":"f7c8518a-3909-4f34-8e42-c16e66054480",
            "name":"Minecraft Server",
            "node":"Server node",
            "sftp_details":{
               "ip":"127.0.0.1",
               "port":2022
            },
            "description":"",
            "limits":{
               "memory":5333,
               "swap":-1,
               "disk":0,
               "io":500,
               "cpu":0
            },
            "invocation":"java -Xms128M -Xmx5333M -jar forge-1.12.2-14.23.5.2854.jar",
            "egg_features":[
               "eula"
            ],
            "feature_limits":{
               "databases":0,
               "allocations":0,
               "backups":0
            },
            "is_suspended":False,
            "is_installing":False,
            "relationships":{
               "allocations":{
                  "object":"list",
                  "data":[
                     {
                        "object":"allocation",
                        "attributes":{
                           "id":1,
                           "ip":"The IP address",
                           "ip_alias":"A Minecraft Server",
                           "port":25565,
                           "notes":"None",
                           "is_default":True
                        }
                     }
                  ]
               },
               "variables":{
                  "object":"list",
                  "data":[
                     {
                        "object":"egg_variable",
                        "attributes":{
                           "name":"Server Jar File",
                           "description":"The name of the Jarfile to use when running Forge Mod.",
                           "env_variable":"SERVER_JARFILE",
                           "default_value":"server.jar",
                           "server_value":"forge-1.12.2-14.23.5.2854.jar",
                           "is_editable":True,
                           "rules":"required|regex:/^([\\w\\d._-]+)(\\.jar)$/"
                        }
                     },
                     {
                        "object":"egg_variable",
                        "attributes":{
                           "name":"Forge version",
                           "description":"The version of minecraft you want to install for.\r\n\r\nLeaving latest will install the latest recommended version.",
                           "env_variable":"MC_VERSION",
                           "default_value":"latest",
                           "server_value":"1.12.2",
                           "is_editable":True,
                           "rules":"required|string|max:9"
                        }
                     },
                     {
                        "object":"egg_variable",
                        "attributes":{
                           "name":"Build Type",
                           "description":"The type of server jar to download from forge.\r\n\r\nValid types are \"recommended\" and \"latest\".",
                           "env_variable":"BUILD_TYPE",
                           "default_value":"recommended",
                           "server_value":"recommended",
                           "is_editable":True,
                           "rules":"required|string|max:20"
                        }
                     },
                     {
                        "object":"egg_variable",
                        "attributes":{
                           "name":"Forge Version",
                           "description":"Gets an exact version.\r\n\r\nEx. 1.15.2-31.2.4\r\n\r\nOverrides MC_VERSION and BUILD_TYPE. If it fails to download the server files it will fail to install.",
                           "env_variable":"FORGE_VERSION",
                           "default_value":"",
                           "server_value":"1.12.2-14.23.5.2854",
                           "is_editable":True,
                           "rules":"required|string|max:20"
                        }
                     }
                  ]
               }
            }
         }
      }
   ],
   "meta":{
      "pagination":{
         "total":1,
         "count":1,
         "per_page":50,
         "current_page":1,
         "total_pages":1,
         "links":{
            
         }
      }
   }
}

請看我的評論 你要問的很簡單,但如果這就是你所需要的,那么你就是 go。 雖然我會在Docker中使用一些東西,這是Pterodactyl使用的。

Pterodactyl 是一個服務器管理平台,它使用 Docker 容器來管理應用程序的實例。 它專為運行、配置和管理無頭游戲服務器(如 Minecraft 服務器)而設計,但也可用於其他應用程序

howeverYoureGettingTheJSONDataVar = { YOUR JSON HERE }

#ALSO YOU SEE THAT BIG 0 BELOW...IT LOOKS LIKE [0] YEAH... THERE ARE MULTIPLE ARRAYS THAN YOU'LL NEED TO USE A LOOP
dataArr = howeverYoureGettingTheJSONDataVar["data"][0]
_IAmLazy = open("A_List_Of_STUFF.csv", "w+") 
_IAmLazy.write(dataArr["attributes"]["name"] + "," + dataArr["attributes"]["uuid"] )
_IAmLazy.close
DataVar = { JSON }

list_data = [x["attributes"]["name"] + "," + x["attributes"]["uuid"] for x in DataVar["data"] if x["attributes"]["name"] and x["attributes"]["uuid"]]

with open('server.csv', 'w+') as file:
   file.writelines(list_data)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM