如何将键创建为文件夹并将值创建为文件 [英] How to create a key as folder and value as files
本文介绍了如何将键创建为文件夹并将值创建为文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
-
我有一个存储桶名称测试文件夹
I have a bucket name testfolder
在测试文件夹内有test1,test2,test3
Inside testfolder there are test1,test2,test3
每个文件夹中都有csv文件
Each folder have there are csv files
需要为文件夹和文件创建键值对
Need to create a key value pair for folder and files
预期
output1 { 'test1':['csv1.csv'], 'test2':['csv2'], 'test3':['csv3']}
output2 { 'test1':'csv1.csv', 'test2':'csv2', 'test3':'csv3'}
#list all the objects
import boto3
s3 = boto3.client("s3")
final_data = {}
all_objects = s3.list_objects(Bucket = 'testfolder')
#List the object in subfolder
#create a dictionary
推荐答案
您可以检查以下给出output1
格式的代码.对于output2
,您可以相应地修改final_data
:
You can check the following code which gives output1
format. For output2
you can modify the final_data
accordingly:
from collections import defaultdict
import boto3
s3 = boto3.resource("s3")
final_data = defaultdict(list)
in_bucket = 'test-bucket-folder-4412313'
for obj in s3.Bucket(in_bucket).objects.all():
print(obj.key)
folder_name = obj.key.split('/')[0]
file_name = obj.key.split('/')[1]
final_data[folder_name].append(file_name)
print(dict(final_data))
它打印出来:
{'test1': ['csv1.csv'], 'test2': ['csv2.csv'], 'test3': ['csv3.csv']}
这篇关于如何将键创建为文件夹并将值创建为文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文