0

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

value,Name,flag
ABC,[email protected]|[email protected],Y
ABC,[email protected],Y
CDE,[email protected],N
CDE,[email protected],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', '[email protected]|[email protected]', 'Y'], ['ABC', '[email protected]', 'Y'], ['CDE', '[email protected]', 'N'], ['CDE', '[email protected]', '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': ['[email protected]','[email protected]','[email protected]'], 'flag': 'Y'},
 {'value': 'CDE', 'name': ['[email protected]','[email protected]'], 'flag': 'N'}]
4
  • 2
    There's nothing AWS or Lambda specific about this. It's just a basic Python coding question. Commented Mar 24, 2021 at 12:41
  • @MarkB , I worte code which is working fine in local system, but when i use obj = s3_read.get_object(Bucket=bucket, Key=key) my code is not working Commented Mar 24, 2021 at 12:50
  • What should happen if two rows with value=ABC actually have different flag values? Commented Mar 24, 2021 at 12:51
  • @sim that detail should have been included in your question then. Commented Mar 24, 2021 at 13:35

1 Answer 1

1

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

current = [['value', 'Name', 'flag'], ['ABC', '[email protected]|[email protected]', 'Y'], ['ABC', '[email protected]', 'Y'],
           ['CDE', '[email protected]', 'N'], ['CDE', '[email protected]', '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': ['[email protected]', '[email protected]'], 'flag': 'Y'}, {'value': 'ABC', 'Name': ['[email protected]'], 'flag': 'Y'}, {'value': 'CDE', 'Name': ['[email protected]'], 'flag': 'N'}, {'value': 'CDE', 'Name': ['[email protected]'], 'flag': 'N'}]
Sign up to request clarification or add additional context in comments.

5 Comments

[{'value': 'ABC', 'name': ['[email protected]','[email protected]','[email protected]'], 'flag': 'Y'}, {'value': 'CDE', 'name': ['[email protected]','[email protected]'], 'flag': 'N'}]
@sim I do not understand your comment. Can you please explain?
I see .. the emails... OK. Code was changed
no its not correct still , please check this one
'value': 'ABC', 'name': ['[email protected]','[email protected]','[email protected]'], 'flag': 'Y

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.