无法使用boto3从s3存储桶读取并向同一存储桶写入不同的文件



我使用下面的Python3 shell代码从S3 bucket中读取数据,提取数据并写入同一bucket中的新文件。但是写入操作不起作用,并且Medicaid_Provider_ID.txt填充了零行。有线索吗??

import logging
import boto3
s3 = boto3.client("s3")
data = s3.get_object(Bucket='mmis.request.file', Key='MEIPASS_FISCAL_TRANS_ONE_RECORD.TXT')
file_lines = data['Body'].iter_lines()
next(file_lines)
new = []
id = 1
for line in file_lines:
line_split = line.decode().split(',')
MEDICAID_PROVIDER_ID = line_split[0]
REASON_CODE = line_split[1]
with open("Medicaid_Provider_ID_.txt","w") as f:
f.writelines(MEDICAID_PROVIDER_ID)
f.close()
id += 1
new = s3.put_object(Bucket='mmis.request.file', Key='Medicaid_Provider_ID_.txt')

每次运行代码时,这行代码都会重新创建您的文件:

with open("Medicaid_Provider_ID_.txt","w") as f:

您应该打开/创建一次文件,然后遍历文件中的所有行,然后在完成后关闭文件。像这样:

import logging
import boto3
s3 = boto3.client("s3")
data = s3.get_object(Bucket='mmis.request.file', Key='MEIPASS_FISCAL_TRANS_ONE_RECORD.TXT')
file_lines = data['Body'].iter_lines()
next(file_lines)
new = []
id = 1
# Open the file
with open("Medicaid_Provider_ID_.txt","w") as f:
# Write each line of the file
for line in file_lines:
line_split = line.decode().split(',')
MEDICAID_PROVIDER_ID = line_split[0]
REASON_CODE = line_split[1]
f.writelines(MEDICAID_PROVIDER_ID)
# Close the file
f.close()
id += 1
new = s3.put_object(Bucket='mmis.request.file', Key='Medicaid_Provider_ID_.txt')

最新更新