数据.json:
{
"bank": 0,
"injury": 0,
"inventory": [],
"money": 110,
"skills": {
"Charmer": {
"description": "Increase by using `/beg` - Increase's your chances in begging to charm the person",
"level": 1,
"per": 0
},
"Escapist": {
"description": "Increase by using `/flood` - Increase chance of escaping robbery attempts and floods!",
"level": 1,
"per": 0
},
"Fishing": {
"description": "Increase by using `/fish` and can increase earnings depending on you're level.",
"level": 1,
"per": 0
},
"Luck": {
"description": "Increaseable by drinking 'Potion of Luck' or using `/coinflip (0.25% chance)` and can change the odds of any minigame in your favor.",
"level": 1,
"per": 0
},
"Master Mind": {
"description": "Be a owner of the bot for more then 1 year - Cannot be increased by command's or editing of database.",
"level": 10,
"per": 100
}
}
}
我需要制作一项新技能并将其添加到当前的技能集中
不知道该怎么做,希望得到任何帮助!
我尝试将字典转换为列表并使用 .append 然后将其转换回字典,但它不起作用
要在字典中创建新项目,您可以执行
dict["[item]"] = "value"
,要将 JSON 文件加载为字典,您可以使用 json
库。因此,您可以按照以下方式进行:
import json
with open("data.json") as data_file:
data = json.load(data_file)
data["skills"]["NewSkill"] = { # Replace with actual name
description = "NewSkill's description" # Replace with actual `description`
level = 10 # Replace with actual `level`
per = 0 # Replace with actual `per`
}
with open("data.json", "w+") as data_file:
json.dump(data, data_file)