【发布时间】:2019-01-21 19:23:41
【问题描述】:
我想从现有的 json 文件中删除我不需要的值:
{ "items": [ { "id": "abcd", "h": 2, "x": 0, "level": 4 }, { "id": "dfgg", "h": 7, "x": 5, "level": 30 } ] }
我已尝试删除适当的值,但得到“迭代期间字典更改大小”。
with open('inventory2.json', 'r') as inf:
data = json.load(inf)
inf.close()
keysiwant = ['x', 'h']
for dic in data['items']:
for k, v in dic.items():
if k not in keysiwant:
dic.pop(k, None)
【问题讨论】:
标签: python python-3.x dictionary