【发布时间】:2021-07-16 21:22:56
【问题描述】:
大约三周前,我开始自学 Python,并且一直在为我的 Ark 服务器为 Red discord bot 开发 RCON cog。我有一个正在尝试格式化的配置,以便我可以使用命令轻松查看服务器的所有设置。
这是我目前为服务器创建的配置
{
"clusters": {
"pvp": {
"joinchannel": 739270285450018927,
"leavechannel": 851819367439400960,
"adminlogchannel": 848933429164507156,
"globalchatchannel": 841118994399494164,
"servers": {
"rag": {
"ip": "192.168.1.201",
"port": 27020,
"password": "passwd",
"chatchannel": 770891102601084928
},
"val": {
"ip": "192.168.1.203",
"port": 27024,
"password": "passwd",
"chatchannel": 770891102601084928
},
"island": {
"ip": "192.168.1.205",
"port": 27021,
"password": "passwd",
"chatchannel": 770891102601084928
}
}
},
"pve": {
"joinchannel": 739270285450018927,
"leavechannel": 851819367439400960,
"adminlogchannel": 848933429164507156,
"globalchatchannel": 841118994399494164,
"servers": {
"rag": {
"ip": "192.168.1.202",
"port": 27022,
"password": "passwd",
"chatchannel": 770891102601084928
},
"val": {
"ip": "192.168.1.204",
"port": 27023,
"password": "passwd",
"chatchannel": 770891102601084928
},
"island": {
"ip": "192.168.1.206",
"port": 27025,
"password": "passwd",
"chatchannel": 770891102601084928
}
}
}
}
}
如果我要打印“服务器设置”,我希望它看起来像这样
PvP 集群
map: rag
ip: 192.168.1.201
port: 27020
password: psswrd
chatchannel: 770891102601084928
map: val
ip: 192.168.1.203
port: 27024
password: psswrd
chatchannel: 770891102601084928
map: island
ip: 192.168.1.205
port: 27021
password: psswrd
chatchannel: 770891102601084928
PvE 集群
map: rag
ip: 192.168.1.202
port: 27022
password: psswrd
chatchannel: 770891102601084928
map: val
ip: 192.168.1.204
port: 27023
password: psswrd
chatchannel: 770891102601084928
map: island
ip: 192.168.1.206
port: 27025
password: psswrd
chatchannel: 770891102601084928
到目前为止我已经尝试过
serverlist = []
settings = await self.config.guild(ctx.guild).all()
for cluster in settings["clusters"]:
for name, server in cluster["servers"].items():
serverlist.extend(settings[cluster]["servers"])
await ctx.send(serverlist)
还有一些其他可能是菜鸟的逻辑,让它按照我想要的方式格式化,但是有这么多嵌套的字典,我迷路了。
最终我会把它做成一个嵌入。但现在我只是想更好地了解如何从我的配置中操作字典以更好地在不和谐中显示它。
我也想一次性打印所有这些作为一条消息。
如果有人能在这方面牵着我的手,并使用我的配置展示一个示例,那将不胜感激:)
【问题讨论】:
-
请用您尝试过的代码更新您的问题。
-
@quamrana 我为我在朋友的帮助下尝试的主要内容编辑了它,但除此之外,我只是不确定如何设置我希望它显示的逻辑。我能想象的最好的方法是 for 循环中的 for 循环,用于迭代集群,然后是集群中的服务器。
标签: python loops dictionary for-loop rcon