I want to read a csv file from S3 make some changes to it, and reupload the file there. All of my code is in lambda so local file can not exist.
I have the code to read the csv and make changes to it. I can not figure out how to put it back. My final data is a list of list where each list represents a row in the csv file.
Here is what I have.
s3 = boto3.client('s3')
obj = s3.get_object(Bucket=bucket_name, Key=myKet)
data = obj['Body'].read().decode('utf-8')
spamreader = csv.reader(io.StringIO(data), delimiter=',')
existing_data = []
for row in spamreader:
existing_data.append(row)
# Add new data to it
existing_data.append([1,2,3])
Any help would be appreciated. Thank you!
CodePudding user response:
Was able to do it like this.
csv_buffer = io.StringIO()
for line in existing_data:
csv_buffer.write(','.join(line) '\n')
s3.put_object(Bucket=bucket_name, Key=myKey, Body=csv_buffer.getvalue())