我有一堆JSON文件(nft集合的元数据(我想做的事:
- 从所有文件中删除dna部分
- 删除空值
过程:
- 读取并解析所有这些文件
- 进行编辑并将其写入同一文件
{
"dna": "xxxxxxxxxxx", #Delete this line from all
"attributes": [
{
"trait_type": "xxxx",
"max_value": null # delete null values
}
],
}
希望这将为您指明正确的方向:
import json
d = json.load(fp)
for key in [x for x in d.keys() if x != 'dna' and d[x] != None]:
value = d[key]
print(key,value)
假设您已使用将json加载到nft
中
with open('<filename>', 'r') as f:
nft = json.load(f)
你可以像一样使用dict.pop(key: Hashable)
删除它们
nft.pop('dna')
if nft.get('attributes'):
for attrs in nft.get('attributes'):
keys = []
for key in attrs:
if attrs[key] is None:
# attrs.pop(key)
keys.append(key)
for key in keys:
attrs.pop(key)
print(nft) # {'attributes': [{'trait_type': 'xxxx'}]}