read csv file in aws lambda python code example

Example: read csv file in aws lambda python

import json
import os
import boto3
import csv

key ='file_name.csv'
bucket ='bucket_name'
def lambda_handler(event,  context):
    s3_resource = boto3.resource('s3')
    s3_object = s3_resource.Object(bucket, key)
    
    data = s3_object.get()['Body'].read().decode('utf-8').splitlines()
    lines = csv.reader(data)
    headers = next(lines)
    print('headers: %s' %(headers))
    for line in lines:
        #print complete line
        print(line)
        #print index wise
        print(line[0], line[1])

Tags:

Misc Example