gpt4 book ai didi

python - 如何使用 Python 在 JSON 中添加键/值?

转载 作者:行者123 更新时间:2023-12-01 03:16:02 25 4
gpt4 key购买 nike

这可能听起来像一个普通问题,但我还没有找到我想要做的事情的好答案。

获取 d.json:

{"SDA":{"Info":{"Description":"Anti Advertisment Bot, Blocks invites extensively.","Download Link":"http://sda.khionu.net/docs/, http://sda.khionu.net/docs/"}}, "Unit 02":{"Info":{"Description":"Server logging bot, c!serverlogs 'server name here if spaces' <# 1-9999>","Download Link":"https://discordapp.com/oauth2/authorize?client_id=222881716606468096&scope=bot&permissions=32768"}}}

我正在尝试将其添加到其中,以逗号分隔:

{'Ctest': {'Info': {'Description': 'Hi', 'Download Link': 'Sure'}}}

我尝试了多种方法,但没有任何效果。这是我当前的代码

a = d[toolname] = {str(toolname):{"Info":{"Description": tooldesc, "Download Link": toollink}}}
f.write(str(a))
f.close()
return jsonify(a), 201

我的全部目标是写

{'Ctest': {'Info': {'Description': 'Hi', 'Download Link': 'Sure'}}} 

像这样的d.json

{"SDA":{"Info":{"Description":"Anti Advertisment Bot, Blocks invites extensively.","Download Link":"http://sda.khionu.net/docs/, http://sda.khionu.net/docs/"}}, "Unit 02":{"Info":{"Description":"Server logging bot, c!serverlogs 'server name here if spaces' <# 1-9999>","Download Link":"https://discordapp.com/oauth2/authorize?client_id=222881716606468096&scope=bot&permissions=32768"}}, {'Ctest': {'Info': {'Description': 'Hi', 'Download Link': 'Sure'}}}

最佳答案

使用json module为此,下面的代码将为您提供线索:

import json
data = json.load('path_to_json_file')
data['key'] = 'value'
json.dump('path_to_json_file', data)

关于python - 如何使用 Python 在 JSON 中添加键/值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42459083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com