首先,我是新来的(第一篇文章),是python的新手
我正在编写一个不和谐的机器人,它调用API,为我的游戏服务器获取一个http请求。API输出是用缩进打印的,它很好,但是不和谐的bot消息都是这样混乱的。我怎么才能修好它?
另外,我无法访问单个行来打印“姓名”或“maxplayers”
这是请求和格式化json的api代码:
import requests
import json
url = "https://vrising-server-scanner.p.rapidapi.com/179.155.101.206/27016"
headers = {
"X-RapidAPI-Key": "03ceea7e11mshe52b1b49827f626p17aa4djsnb92582243b8e",
"X-RapidAPI-Host": "vrising-server-scanner.p.rapidapi.com"
}
response = requests.get(url, headers=headers)
data = response.json()
json_formatted = json.dumps(data, indent=4)
print(json_formatted)发送API输出的不一致代码:
@client.command()
async def status(ctx):
await ctx.send('Checking status...')
await asyncio.sleep(1)
await ctx.send('Status:')
await asyncio.sleep(0.1)
await ctx.send('```')
await asyncio.sleep(0.1)
await ctx.send(subprocess.check_output(['python', 'VrisingAPI.py']))
await asyncio.sleep(0.1)
await ctx.send('```')
client.run(TOKEN)发布于 2022-09-01 18:51:58
由于您没有为check_output()定义任何特殊的编码,所以默认情况下它总是返回一个bytes对象。在您的情况下,可以使用UTF-8作为编码:
subprocess.check_output(['python', 'VrisingAPI.py'], encoding='UTF-8')另一个可能的解决方案是使用bytes.decode()手动解码它。
subprocess.check_output(['python', 'VrisingAPI.py']).decode("utf-8")如果使用Python3.7或更高版本,也可以将参数text设置为True,这将使用系统的默认编码对其进行解码:
subprocess.check_output(['python', 'VrisingAPI.py'], text=True)https://stackoverflow.com/questions/73573968
复制相似问题