我正在接收来自外部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格式传递它,以便进一步处理。到目前为止,我已经通过了one、two、three和four线程。什么都不足以解决问题。你知道吗
从路径中,我需要通过以下方式获得Dictionary/JSON格式:
{
"curation":{
"google-search-images":[
{
"name":"pic0"
},
{
"name":"pic1"
}
]
},
"derived":{
"recognition-matches":[
{
"name":"img2"
}
],
"errors":[
{
"name":"foo"
}
]
}
}
在上面的Dictionary/JSON中,curation
、google-search-images
、this_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.
任何帮助以外,我的代码是欢迎的!你知道吗
目前没有回答
相关问题 更多 >
编程相关推荐