【问题标题】:Store a list of specific values from json output into a text file in Python将 json 输出中的特定值列表存储到 Python 中的文本文件中
【发布时间】:2021-01-03 06:47:38
【问题描述】:

我正在使用翼手龙来托管我的 Minecraft 服务器,并且我一直在使用 API 来制作一个工具来控制它,因此我不必在托管它的机器上运行或登录到 minecraft。我希望能够从 api 输出的 json 输出中获取服务器名称及其各自的 UUID,并将它们存储在一个文件中,以便在我需要指定要对哪个服务器执行某个操作时可以引用它们。有没有办法可以制作这样的过滤列表并在我想要执行操作时引用它?

这是一个输出示例:

{
   "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":{
            
         }
      }
   }
}

【问题讨论】:

  • 你有什么尝试吗?只需少量代码即可。 json 的工作方式类似于 dictionary,因此您可以轻松访问它。并且打开/创建一个文本文件也很容易完成。向我们展示你的努力。
  • 看看我很好奇的是,你是如何将输出输入 python 的?看,作为创建 Minecraft 服务器的人,最有可能的是,输出是通过终端,并且您需要一些管道(例如 Grep)来实际执行您的要求。这也更像是一个 Docker 问题。除非您手动将 JSON 粘贴到 Python 脚本中……在这种情况下……嗯,是的。但是你要求做的事情可以在 python 中用 5 行或更少的代码来完成
  • @Greg432 它是通过 api 完成的,并与开发人员也为该 api 制作的 python 包装器一起接收

标签: python json python-3.x server minecraft


【解决方案1】:
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)

【讨论】:

    【解决方案2】:

    请看我的comment。你要问的很简单,但如果这就是你所需要的,那么你就去吧。虽然我会在 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
    

    【讨论】:

      猜你喜欢
      • 2017-10-26
      • 1970-01-01
      • 2021-06-01
      • 2021-04-24
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多