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

从字典数组中检索值

  •  0
  • Goku  · 技术社区  · 3 年前

    我正在尝试处理一个json文件,如下面所示,并以下面的输出格式提取其数据以供进一步处理。

    json文件

    {
        "application_robotics-2.1.610.80350109": [
            "/home/machine_process/application_robotics/services/linear_service/4.106.50109987/robotics.yaml",
            "/home/machine_process/application_robotics/services/linear_service/4.106.50109987/application_robotics-4.106.50109987.zip"
        ],
        "web_robotics-3.116.50100987": [
            "/home/machine_process/application_robotics/services/web_robotics/3.116.50100987/robotics.yaml",
            "/home/machine_process/application_robotics/services/web_robotics/3.116.50100987/web_robotics-3.116.50100987.zip"
        ]
    }
    

    预期输出格式

    name = "application_robotics-2.1.610.80350109" # where name is a variable to be used in the other portion of the code.
    yaml = "/home/machine_process/application_robotics/services/linear_service/4.106.50109987/robotics.yaml" # where yaml is a variable.
    zip = "/home/machine_process/application_robotics/services/linear_service/4.106.50109987/application_robotics-4.106.50109987.zip"  # where zip is a variable.
    

    其他条目采用相同的格式。

    下面是我想出的代码片段,我并不完全理解其中的逻辑。这里的任何帮助都会非常有用。谢谢

    with concurrent.futures.ProcessPoolExecutor() as executor:
        with open(file_path, "r") as input_json:
            json_data = json.load(input_json)
            for key, value in json_data.items():
                name = json_data[key]
                yaml = json_data[value]
                zip = json_data[value]
                file_location = os.path.dirname(tar)
                futures = executor.submit(
                    other_function_name, yaml, zip, file_location, name
                )
                results.append(futures)
    

    电流输出:

    ['home/machine_process/application_robotics/services/linear_service/4.106.50109987/robotics.yaml', '/home/machine_process/application_robotics/services/linear_service/4.106.50109987/application_robotics-4.106.50109987.zip']
    
    1 回复  |  直到 3 年前
        1
  •  2
  •   enke    3 年前

    自从 name 对应于按键; yaml 到列表的第一个元素;和 zip_ 到第二个元素(注意 zip 是python内置的,因此避免将其用作变量名),我们可以在循环字典并将其传递给 executor .

    with concurrent.futures.ProcessPoolExecutor() as executor:
        with open(file_path, "r") as input_json:
            json_data = json.load(input_json)
            for name, (yaml, zip_) in json_data.items():
                file_location = os.path.dirname(tar)
                futures = executor.submit(other_function_name, yaml, zip_, file_location, name)
                results.append(futures)