简体   繁体   中英

How to read object and convert to json in AWS lambda python

My csv is below. I need to convert into json using AWS lambda using python

value,Name,flag
ABC,ab@gmail.com|cd@gmail.com,Y
ABC,de@gmail.com,Y
CDE,ab@gmail.com,N
CDE,cd@gmail.com,N

Code is below

import boto3
import csv
s3_read = boto3.client('s3')
obj = s3_read.get_object(Bucket=bucket, Key=key)
CSVcontent = obj['Body'].read().decode('utf-8').split('\n')
CSVfile = csv.reader(CSVcontent)
print('CSVfile', list(CSVfile))

My current out is

[['value', 'Name', 'flag'], ['ABC', 'ab@gmail.com|cd@gmail.com', 'Y'], ['ABC', 'de@gmail.com', 'Y'], ['CDE', 'ab@gmail.com', 'N'], ['CDE', 'cd@gmail.com', 'N']]
  • I need to merge all the same keys and same flag
  • I need to change all the | to different string

Expected out is below

[{'value': 'ABC', 'name': ['ab@gmail.com','cd@gmail.com','de@gmail.com'], 'flag': 'Y'},
 {'value': 'CDE', 'name': ['ab@gmail.com','cd@gmail.com'], 'flag': 'N'}]

Starting with your current data ( list(CSVfile) ) and creating a list of dicts

current = [['value', 'Name', 'flag'], ['ABC', 'ab@gmail.com|cd@gmail.com', 'Y'], ['ABC', 'de@gmail.com', 'Y'],
           ['CDE', 'ab@gmail.com', 'N'], ['CDE', 'cd@gmail.com', 'N']]


def handle(k, v):
    if k == 'Name':
        return v.split('|')
    else:
        return v


data = []
for entry in current[1:]:
    data.append({k: handle(k,entry[idx]) for idx, k in enumerate(current[0])})
print(data)

output

[{'value': 'ABC', 'Name': ['ab@gmail.com', 'cd@gmail.com'], 'flag': 'Y'}, {'value': 'ABC', 'Name': ['de@gmail.com'], 'flag': 'Y'}, {'value': 'CDE', 'Name': ['ab@gmail.com'], 'flag': 'N'}, {'value': 'CDE', 'Name': ['cd@gmail.com'], 'flag': 'N'}]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM