ArchieTiger
ArchieTiger

Reputation: 2253

How to parse data from amazon lambda's response['Body']

I'm using aws lambda to load a csv file stored in s3, how can I read the content of the response line by line, as one would read from csv.reader() i.e.

reader = csv.reader(f)
for line in reader:
    #do something
    line[1] = line[1].lower()

Lambda function:

import urllib
import boto3

print('Loading function')

s3 = boto3.client('s3')

def lambda_handler(event, context):
    bucket = event['Records'][0]['s3']['bucket']['name']
    key = urllib.unquote_plus(event['Records'][0]['s3']['object']['key'].encode('utf8'))

    response = s3.get_object(Bucket=bucket, Key=key)
    content = response['Body'].read()

    #read content line by line as a list

Upvotes: 0

Views: 943

Answers (1)

byumark
byumark

Reputation: 298

Would content.splitlines() work for you?

for line in content.splitlines():
    # do something with line

Upvotes: 1

Related Questions