python json使用来自另一个搜索的值进行查找

时间:2018-04-20 19:03:25

标签: python json

新手在这里,通常道歉。所以我非常基础和完全自学(=通过从网站复制代码来学习,主要是在这里)。

==>背景< == 最终,我试图创建taskwarrior依赖关系的分层显示。我认为我需要完成的步骤是:

  1. TW导出 - > JSON
  2. JSON导入到python
  3. 找到关键所在的条目'存在(这些是父任务)
  4. 提取'取决于' pair(这些是子任务的UUID)
  5. 在导入的JSON中查找这些值(以获取子任务描述)
  6. 以漂亮的,可读的顺序显示
  7. 我已经陷入了第5步。

    ==>问题< == 我应该如何构建我的查找逻辑呢?

    非常感谢提前。

    WGII

    import json
    
    inputfile=open('data_file.json', encoding='utf-8')
    pydata=json.load(inputfile)
    
    for parent in pydata:
        if "depends" in parent:
            print(parent['description'])
            for child in pydata:
                if child['uuid'] = parent[depends]:
                    print(child['description'])
    

    ---编辑---添加错误信息&编号为编辑行:

    Traceback (most recent call last):
      File "~/json0.py", line 10, in <module>
        if child['uuid'] == parent[depends]:
    NameError: name 'depends' is not defined
    

    json:

     [
     {"id":1,"depends":["e6e559c6-742d-4862-9eca-a0f100e1cdb9"],"description":"redacted_1","entry":"20180406T204008Z","modified":"20180406T211650Z","status":"pending","uuid":"842003ce-b52f-4a1c-b6b2-7586c5e5defe","urgency":-4.92877},
     {"id":2,"description":"redacted_2","entry":"20180406T211113Z","modified":"20180406T215713Z","status":"pending","uuid":"e6e559c6-742d-4862-9eca-a0f100e1cdb9","urgency":8.07123},
     {"id":3,"description":"redacted_3","entry":"20180406T211224Z","modified":"20180406T221728Z","status":"pending","uuid":"184c1e8b-acf0-4cf3-b2d5-8130e5cb2cca","urgency":8.07123},
     {"id":4,"description":"redacted_4","due":"20180404T230000Z","entry":"20180406T215603Z","modified":"20180406T222147Z","priority":"H","start":"20180406T222147Z","status":"pending","uuid":"770aa058-ba48-453c-8070-6bb32279e7c8","urgency":22.0712},
     {"id":5,"description":"redacted_5","entry":"20180406T223551Z","modified":"20180406T223951Z","start":"20180404T230000Z","status":"pending","uuid":"69e44b6e-0a55-48d7-b659-352753ae19ae","annotations":[{"entry":"20180406T223716Z","description":"now"}],"urgency":4.87123},
     {"id":6,"description":"redacted_6","entry":"20180420T123738Z","modified":"20180420T123738Z","status":"pending","uuid":"0c470063-92ae-4893-b7de-bd9b9f94fb04","urgency":0}
     ]
    

    一些系统信息: xubuntu 16.04 thonny 2.1.16 python 3.6.4

    -----编辑2 ---------------遵循Prateek的帮助-------------

    好的,我越来越近了:

    import json
    
    inputfile=open('data_file.json', encoding='utf-8')
    pydata=json.load(inputfile)
    
    ID_descr = {}
    for z in pydata:
        ID_descr[z['uuid']]= [z['description']]
    
    for parent in pydata:
        if "depends" in parent:   
            print(parent['description'])
            childNo=(parent['depends'])
            print(childNo)
            print(ID_descr[(childNo)]) 
    

    输出+错误消息:

    redacted_1
    ['e6e559c6-742d-4862-9eca-a0f100e1cdb9']
    Traceback (most recent call last):
      File "~/json0.py", line 17, in <module>
        print(ID_descr[(childNo)])
    TypeError: unhashable type: 'list'
    

    尝试了最后一行的几种变体,但无法弄清楚如何使用ID_descr dict查找来处理变量&#39;。

    例如。带/不带括号&amp;有/无&#34; &#34;或者&#39; &#39; - 没有帮助

    例如。我试图将字符串化childNo=str(parent['depends'])

    但这只是给我一个差异错误:KeyError: "['e6e559c6-742d-4862-9eca-a0f100e1cdb9']"

1 个答案:

答案 0 :(得分:0)

json在pydata转换为字典要获取内部值,您需要根据需要解析字典。

  1. 首先将所有子ID保存在列表或词典中
  2. 打印这些儿童ID的说明。
  3. 代码

    child_task_dic = dict()
    # Get all child ids from JSON
    for parent in pydata:
            # print('id ' + str(parent['id']) + ':' + parent['description'])
            if 'depends' in parent:
                child_task_dic[parent['id']]= parent['depends']
    
    for k,v in child_task_dic.items():
        print('Parent task id : ' + str(k))
        for y in pydata:
            if y['uuid'] in v:
                print('\t' + 'Child task id ' + y['uuid'])
                print('\tStatus : ' + y['description'])
    

    输出

    {1: ['e6e559c6-742d-4862-9eca-a0f100e1cdb9']}
    Parent task id : 1
        Child task id e6e559c6-742d-4862-9eca-a0f100e1cdb9
        Status : redacted