简体   繁体   中英

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

I am using pterodactyl to host my minecraft servers and I have been using the API to make a tool to control it so I don't have to go on the machine that hosts it or be logged into minecraft. I want to be able to grab the server names and their respective UUIDs from the json output the api shoots out and store them in a file so that they can be referenced when I need to specify which server I want to do a certain action to. Is there a way I can make a filtered list like this and reference it when I want to do an action?

Here is an example of the 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":{
            
         }
      }
   }
}

Please see my comment . What you're asking is very simple, but if thats all you need than here you go. Although I'd use something in Docker which is what Pterodactyl uses.

Pterodactyl is a server management platform that uses Docker containers to manage instances of applications. It's designed for running, configuring, and managing headless game servers, like Minecraft servers, but can be used for other applications as well

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM