从字典数组中检索值

悟空

我正在尝试处理如下所示的 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']
寡妇

由于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)

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章