python - 删除 json 文件中所有出现的键
问题描述
如何删除 json 文件中所有出现的键?在下面的示例中,我想删除所有“评级”键。
现在怎么样了:
{
"player": {
"rating": "99",
"rarity": "super_rare"
},
"player2": {
"rating": "87",
"rarity": "rare"
}
}
我想要的是:
{
"player": {
"rarity": "super_rare"
},
"player2": {
"rarity": "rare"
}
}
解决方案
尝试这个:
import json
with open('data.json') as fp:
data = json.loads(fp.read())
for player in data.values():
del player['rating']
with open('output.json', 'w') as fw:
json.dump(data, fw, indent=4)
输出:
{'abc': {'rarity': 'super_rare'}, 'efg': {'rarity': 'rare'}}
推荐阅读
- php - PHP:如何随机选择最大概率值?
- python - 调整存储在数组中的所有图像的大小
- c# - Visual Studio SDK 获取类型修饰符信息 - 类型是抽象的还是内部的?
- python - 将数据从 HTML 插入到 Django 模板
- c# - 使用操作从任务更新 UI
- java - 这是否可以使用 Selenium 和 Java 来自动化 Angular Web 应用程序?
- asp.net-core - Protobuf 不工作 - Jetbrains Rider .NET Core 3.1
- pandas - 如何替换 dtype=K.floatx()?
- angular - 我们可以将 TestBed 用于带有 NativeScript 的服务吗?
- c++ - C++ 过程入口点 CLRCreateInstance 不能位于动态链接库 mscoree.dll 中