【发布时间】:2020-06-07 20:17:12
【问题描述】:
我是 json 文件语法的新手,我想知道如何从我的 json 文件中删除“phones”及其值,以便 end、start、alignedWord 和 word 是我在 words 键中的唯一值。
我的代码:
with open('dest_file.json', 'w') as dest_file:
with open('test1.json', 'r') as j:
contents = json.loads(j.read())
for value in contents['words']:
del(value['case'])
del(value['endOffset'])
del(value['startOffset'])
dest_file.write(json.dumps(contents,indent = 4))
示例 JSON 对象:
"words": [
{
"alignedWord": "the",
"end": 6.31,
"phones": [
{
"duration": 0.09,
"phone": "dh_B"
},
{
"duration": 0.05,
"phone": "iy_E"
}
],
"start": 6.17,
"word": "The"
},
除此之外,手机究竟是什么数据类型,为什么我无法通过我当前的代码删除它?
【问题讨论】:
标签: python json with-statement del