aws3 Python读取、处理,然后Wri

2024-06-07 07:10:21 发布

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

我希望使用boto3甚至类似Python中的smart_open从S3中逐行读取一个文件,然后处理每一行(例如清除某些字段),然后将这些行写回S3。关键是不要在内存中保存任何数据。有什么建议吗?我试过使用下面的方法,但没有成功

into = "s3://"+access_key+":"+secret_key+"@"+bucket+"/Filetoread.csv"
out = "s3://"+access_key+":"+secret_key+"@"+bucket+"/Filetowrite.csv"

def streamline(inputfile, outputfile):
with smart_open.smart_open(inputfile, 'r') as infile, smart_open.smart_open(outputfile, 'w') as outfile:
    for line in infile:
        outfile.write(line + '\n')

streamline(into, out)

Tags: csvkeysecrets3bucketaccesssmartas