首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从字典数组中检索值

从字典数组中检索值
EN

Stack Overflow用户
提问于 2022-04-30 06:07:20
回答 1查看 43关注 0票数 0

我试图像下面这样处理json文件,并以下面的输出格式提取它的数据以供进一步处理。

json文件

代码语言:javascript
复制
{
    "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"
    ]
}

预期输出格式

代码语言:javascript
复制
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.

同样的格式也适用于其他条目。

下面是我想出的代码片段,但我并没有完全理解逻辑。任何帮助在这里都会很有帮助。谢谢。

代码语言:javascript
复制
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)

当前产出:

代码语言:javascript
复制
['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']
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-30 06:25:14

由于name对应于键;yaml对应于列表的第一个元素;zip_对应于第二个元素(请注意,zip是python内置的,因此避免将其用作变量名),因此我们可以在遍历字典并将它们传递给executor时直接解压它。

代码语言:javascript
复制
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)
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72066120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档