代码之家  ›  专栏  ›  技术社区  ›  Aavik

附加听写列表

  •  -1
  • Aavik  · 技术社区  · 6 年前

    我正在编写一个具有重启功能的Python程序。我想以JSON格式存储程序执行的状态,以便在重新启动时查询JSON并从失败点重新启动。

    {
        "job_name": xxxxx,
        "job_start_time": xxxxx,
        "site": xxxxxx,
        "tasks": [
            {
                "id": <unique id to look-up on restart>
                "task_start_time": 
                "task_end_time":
                "runtime":
                "successful": <true or false>
                "error_message":<if successful is false>
            }
        ]   
    }
    

    当阶段成功完成时,它会将任务dict追加到任务列表中。

    我的问题是如何在保留整个python对象的同时附加任务字典。

    在JSON中可能吗?

    3 回复  |  直到 6 年前
        1
  •  1
  •   Burhan Khalid    6 年前

    你可以使用 update 修改对象的字典方法。下面是一个例子:

    d = {'inventory': [{'Color': 'Brown', 'Model': 'Camry', 'Year': 2018},
                   {'Model': 'Corolla', 'Year': 2017}],
         'name': 'Toyota'}
    
    d['inventory'][0].update({'Doors': 4})
    print(d)
    {'inventory': [{'Color': 'Brown', 'Doors': 4, 'Model': 'Camry', 'Year': 2018},
                   {'Model': 'Corolla', 'Year': 2017}],
     'name': 'Toyota'}
    
        2
  •  0
  •   NightOwl19    6 年前

    {
        "job_name": xxxxx_2,
        "job_start_time": xxxxx,
        "site": xxxxxx,
        "tasks": [
            {
                "id": <unique id to look-up on restart>
                "task_start_time": 
                "task_end_time":
                "runtime":
                "successful": <true or false>
                "error_message":<if successful is false>,
                "parameters":{
                 "--Dict":{dict_values}
    }
            }
        ]   
    }
    
        3
  •  0
  •   John Luscombe    6 年前

    dictionary["tasks"].append(whatever)