Home > Enterprise >  Edit dict in string
Edit dict in string

Time:12-25

I want to edit string data

ids = [12,34,56,78]
data = '{"id":999}'

I want to replace '999' with every id in and want output to be:

'{"id":12}'
'{"id":34}'
'{"id":56}'
'{"id":78}'

I tried with .format but it is not working.

CodePudding user response:

Hi you need to iterate over the array and replace the 999.

ids = [12,34,56,78]
data = '{"id":999}'

for i in ids:
    print(data.replace('999',str(i)))

CodePudding user response:

I don't understand your question completely, but I hope this code is helpful.

ids = [12,34,56,78]
data = '{"id":999}'
IDS = []
dataeval = eval(data)
for id in ids:
   dataeval["id"] = id
   IDS.append(str(dataeval))
  • Related