【发布时间】:2020-05-03 15:25:19
【问题描述】:
我正在尝试将我的 bucket.name 和 obj.key 的值写入一个 csv 文件,并在其各自的列中使用其各自的值。
当我运行下面的代码时,它只向我的 csv 文件输出一行:
bucket_name | key_s3_path
value , value
但是我的 ff 变量的输出是:
{'bucket_name': ["'mybucket', "], 'key_s3_path'["'my/file/path/80ece.txt', "]}
{'bucket_name': ["'mybucket', "], 'key_s3_path': ["'my/file/path/80ece.txt', "]}
{'bucket_name': ["'mybucket', "], 'key_s3_path': ["'my/file/path/80ece.txt', "]}
如您所见,存在三个值,但它只将三个值中的一个写入 csv 文件。
这是我的代码:
def load_into_csv(years):
for year in years:
for buckett in s3.buckets.all():
for aobj in buckett.objects.filter(Bucket=bucket,Prefix=PREFIX):
if year in aobj.key:
f= "'{}', ".format(buckett.name)
ff= "'{}', ".format(aobj.key)
#print(f)
dd = {'bucket_name':[f], 'key_s3_path':[ff]}
#print(dd)
df = pd.DataFrame(data=dd)
#print(df)
export_csv = df.to_csv (r'my/path/0177_s3_files_v2.csv', index = None, header=True)
years=['2017']
load_into_csv(years)
关于为什么会发生这种情况的任何想法?
【问题讨论】:
标签: python-3.x pandas numpy csv boto3