Skip to content

Instantly share code, notes, and snippets.

@alecbw
Created January 15, 2021 06:41
Show Gist options
  • Save alecbw/a8d35b92c76346b8cd147136fd8f3ac6 to your computer and use it in GitHub Desktop.
Save alecbw/a8d35b92c76346b8cd147136fd8f3ac6 to your computer and use it in GitHub Desktop.
get CSV from S3 Bucket as list of dictionaries
import boto3
import csv
def get_csv_from_s3_as_list_of_dictionaries(bucket_name, filename):
s3_obj = boto3.client("s3").get_object(Bucket=bucket_name, Key=filename.lstrip("/"))["Body"]
return [{k:v for k, v in row.items()} for row in csv.DictReader(s3_obj.read().decode('utf-8').splitlines(True), skipinitialspace=True)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment