Home > Enterprise >  Convert string data to dictionary inside list
Convert string data to dictionary inside list

Time:05-25

From a script, I am getting data like that is given below.

Neha, 30,A
Monika ,22,B
Anni,33,C

I want to convert this data in a given way that is given below.

[{'name':Neha,'age':30,'grade':A},{'name':Monika,'age':22,'grade':B},{'name':Anni,'age':33,'grade':c}]

Can anyone please help me to get the data in this way any suggestion . Thanks in advance

CodePudding user response:

This is your solution

s = """Neha,30,A
Monika,22,B
Anni,33,C"""

# print(s.split('\n'))

l = []

s = s.split('\n')

for i in s:
    # print(i)
    temp = i.split(',')
    # print(temp)
    d = {}
    d['name'] = temp[0]
    d['age'] = temp[1]
    d['grade'] = temp[2]
    l.append(d)
print(l)
  • Related