May-14-2020, 10:04 AM
I am trying to read a csv file from S3 bucket and store its content into a dictionary. Sample csv file data. I want to use my first row as key and subsequent rows as value
sample data:
name,origin,dest
xxx,uk,france
yyyy,norway,finland
zzzz,denmark,canada
I am using the below code which is storing the entire row in a dictionary. But I want to loop through each row and store each field in a row as key value pair.
Current output: {'content': 'xxx,uk,france'}
Required output: {'name':'xxx','origin':'uk','dest':'france'}
sample data:
name,origin,dest
xxx,uk,france
yyyy,norway,finland
zzzz,denmark,canada
I am using the below code which is storing the entire row in a dictionary. But I want to loop through each row and store each field in a row as key value pair.
Current output: {'content': 'xxx,uk,france'}
Required output: {'name':'xxx','origin':'uk','dest':'france'}
import boto3
s3 = boto3.client('s3')
obj = s3.get_object(Bucket = 'bucket_name', Key = 'logs/log.csv')
lines = obj['Body'].read().decode("utf-8").replace("'", '"')
lines = lines.splitlines()
if (isinstance(lines, str)):
lines = (lines)
docData = {}
for line in lines:
docData['content'] = str(line)
print(docData)
