【问题标题】:How to retrieve "values" against specific "key" in nested dictionary如何针对嵌套字典中的特定“键”检索“值”
【发布时间】:2021-10-14 20:52:42
【问题描述】:

我有一个类似于 JSON_FILE 的 json 文件:

它包含嵌套字典。我想检索密钥注释(在文件中出现一次)。特别是针对键 image_id 的所有 (在文件中多次出现)并将其存储在单独的文件中。我如何在 PYTHON

中做到这一点

【问题讨论】:

  • 请在寻求帮助之前尝试一下。
  • 将json写入文件有answer

标签: python json dictionary key key-value


【解决方案1】:

我已经能够解决这个问题

import json
    
with open("myfile.json") as f:
  data_retreived= json.load(f) 
a=data_retreived["annotations"]
    
    
        
myfile = open('data.txt', 'w') 
for f in a:   
  myfile.write("%s\n" % f['image_id'])   
  #print(f['image_id']) 
myfile.close()

【讨论】:

    猜你喜欢
    • 2020-10-01
    • 2018-09-27
    • 2022-01-15
    • 2013-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-16
    • 2016-11-14
    相关资源
    最近更新 更多