将 json 输出中的特定值列表存储到 Python 中的文本文件中
Store a list of specific values from json output into a text file in Python
我正在使用翼手龙来托管我的 Minecraft 服务器,并且我一直在使用 API 制作一个工具来控制它,这样我就不必进入托管它的机器或登录我的世界。我希望能够从 json 输出中获取服务器名称及其各自的 UUID,api 输出并将它们存储在一个文件中,以便在我需要指定哪个服务器时可以引用它们想做某一个动作。有没有一种方法可以像这样制作过滤列表并在我想执行操作时引用它?
这是一个输出示例:
{
"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":{
}
}
}
}
请看我的。你问的很简单,但如果这就是你所需要的,那么你就去吧。尽管我会在 Docker
中使用一些东西,而这正是 Pterodactyl
使用的东西。
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)
我正在使用翼手龙来托管我的 Minecraft 服务器,并且我一直在使用 API 制作一个工具来控制它,这样我就不必进入托管它的机器或登录我的世界。我希望能够从 json 输出中获取服务器名称及其各自的 UUID,api 输出并将它们存储在一个文件中,以便在我需要指定哪个服务器时可以引用它们想做某一个动作。有没有一种方法可以像这样制作过滤列表并在我想执行操作时引用它?
这是一个输出示例:
{
"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":{
}
}
}
}
请看我的Docker
中使用一些东西,而这正是 Pterodactyl
使用的东西。
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)