如果存在相同的键,则将列表转换为递归字典和值列表

2024-06-16 11:27:17 发布

您现在位置:Python中文网/ 问答频道 /正文

输入

我正在接收来自外部JSON源的输入,其中包含路径。遵循以下步骤:

datalake-dev/facial_recognition/
datalake-dev/facial_recognition/curation/google-search-images/this_is_a_dir.png/pic0.jpg
datalake-dev/facial_recognition/curation/google-search-images/this_is_a_dir.png/pic1.jpg
datalake-dev/facial_recognition/curation/google-search-images/this_is_a_dir.png/pic10.png
datalake-dev/facial_recognition/curation/google-search-images/this_is_a_dir.png/pic11.jpg
datalake-dev/facial_recognition/curation/google-search-images/this_is_a_dir.png/pic12.png
datalake-dev/facial_recognition/curation/google-search-images/this_is_a_dir.png/pic13.jpg
datalake-dev/facial_recognition/landing/input-images/
datalake-dev/facial_recognition/landing/input-images/this_is_a_dir.png

帮助

因此,我需要以API/JSON/Dictionary格式传递它,以便进一步处理。到目前为止,我已经通过了onetwothreefour线程。什么都不足以解决问题。你知道吗

所需输出

从路径中,我需要通过以下方式获得Dictionary/JSON格式:

{

    "curation":{
        "google-search-images":[
            {
                "name":"pic0"
            },
            {
                "name":"pic1"
            }
        ]
    },
    "derived":{
        "recognition-matches":[
            {
                "name":"img2"
            }
        ],
        "errors":[
            {
                "name":"foo"
            }
        ]
    }

}

在上面的Dictionary/JSON中,curationgoogle-search-imagesthis_is_a_dir.png都是目录。我需要一些东西,根据这些路径的长度递归地将它们放入字典。你知道吗

我的审判

for contents in result['Contents']:

    directory_or_file_list = contents['Key'].split('/')  # To identify if the path is pointing as file / directory
    path = contents['Key']

    splitted_path = path.split('/')
    # ['datalake-dev', 'facial_recognition', 'landing', 'input-images', 'this_is_a_dir.png', 'pic0.jpg']

    if '' in splitted_path:
        splitted_path.pop()
        all_paths.append(splitted_path)
        # The object 'api' holds the dictionary expected.
        api[splitted_path[0]] = splitted_path[1]
        # api[splitted_path[0]] = {splitted_path[1] : {splitted_path[2] : [append_all_elements_under_this]} }

    if directory_or_file_list[-1].split('.')[-1] in ['jpg', 'jpeg', 'png', 'tiff']:
        print(path)

    else:
        print(path)

Note: Perhaps there is a way to hard code, but then I wouldn't post it here it that'd be the case. Also, no chance of using os.walk(). Been there done that. It isn't OS File system.

任何帮助以外,我的代码是欢迎的!你知道吗


Tags: pathdevsearchpngisdirgooglethis