First, take a look at my code below.
import string
DNA=["Alpha", "Bravo", "Charlie", "Delta", "Echo", "CharlieChoo", "DeltaAir", "Alpha bet", "ChooChoo", "Airline"]
body = "{\"startDate\":\"2016-01-01\"\
,\"endDate\":\"2017-10-30\"\
,\"timeUnit\":\"date\"\
,\"keywordGroups\":[{\"groupName\":\"Alpha\",\"keywords\":[\"Alpha\"]}\
,{\"groupName\":\"Bravo\",\"keywords\":[\"Bravo\"]}\
,{\"groupName\":\"Charlie\",\"keywords\":[\"Charlie\"]}\
,{\"groupName\":\"Delta\",\"keywords\":[\"Delta\"]}\
,{\"groupName\":\"Echo\",\"keywords\":[\"Echo\"]}]\
,\"device\":\"\",\"ages\":[\"1\",\"11\"],\"gender\":\"\"}"
body = body.replace(DNA[0],DNA[5],2)
body = body.replace(DNA[1],DNA[6],2)
body = body.replace(DNA[2],DNA[7],2)
body = body.replace(DNA[3],DNA[8],2)
body = body.replace(DNA[4],DNA[9],2)
body
and the output is below
'{"startDate":"2016-01-01","endDate":"2017-10-30","timeUnit":"date","keywordGroups":
[{"groupName":"Alpha betChoo","keywords":["Alpha betChoo"]},
{"groupName":"ChooChooAir","keywords":["ChooChooAir"]},
{"groupName":"Charlie","keywords":["Charlie"]},
{"groupName":"Delta","keywords":["Delta"]},
{"groupName":"Airline","keywords":["Airline"]}],"device":"","ages":
["1","11"],"gender":""}'
My intended output is below
#body = "{\"startDate\":\"2016-01-01\"\
#,\"endDate\":\"2017-10-30\"\
#,\"timeUnit\":\"date\"\
#,\"keywordGroups\":[{\"groupName\":\"CharlieChoo\",\"keywords\":[\"CharlieChoo\"]}\
#,{\"groupName\":\"DeltaAir\",\"keywords\":[\"DeltaAir\"]}\
#,{\"groupName\":\"Alpha bet\",\"keywords\":[\"Alpha bet\"]}\
#,{\"groupName\":\"ChooChoo\",\"keywords\":[\"ChooChoo\"]}\
#,{\"groupName\":\"Airline\",\"keywords\":[\"Airline\"]}]\
#,\"device\":\"\",\"ages\":[\"1\",\"11\"],\"gender\":\"\"}"
So basically I was trying to replace groupName and keywords from DNA list. In this example I only have 10 obj in DNA list, but my real projects contains couple thousands.
My personal thought is that replacing strings are not appropriate because the strings is likely to be overlapping. Is there another way to do my task? One thing to consider is that I need to have my output as same type of first body string (only the words are changed). Thanks in advance
--------------------------------------EDIT---------------------------------------------------------------
New error occured regarding @AJAX1234 answer.
import pandas as pd
import json
#reading xlsx file
ex = pd.ExcelFile('mat_hierarchy.xlsx').parse('Sheet1')
DNA = ex.loc[:,'4Level']
DNA
Above is my DNA files and below is output
0 Fruit
1 MixFruit
2 SuperFruit
3 PassionFruit
4 Orange
5 Lemon
6 Mango
................. it goes on forever :(
Using this information, I ran your code and "name a is not defined" error is keep showing. I am only beginner but my best guess is that my "DNA" is defined as indexes (DNA.index[0] or etc..) and I have changed your code "a" with numbers, and it still wont work.
Any suggestion regarding this problem? Thanks for the input!!!
------------------------EDIT 2-------------------------------
body_intro = "{\"startDate\":\"2016-01-01\",\"endDate\":\"2017-10-30\",\"timeUnit\":\"date\",\"keywordGroups\":[{\"groupName\":\""
body_keywords = "\",\"keywords\":[\""
body_groupName = "\"]},{\"groupName\":\""
body_last = "\"]}],\"device\":\"\",\"ages\":[\"1\",\"2\",\"3\",\"4\",\"5\",\"6\",\"7\",\"8\",\"9\",\"10\",\"11\"],\"gender\":\"f\"}"
for i in range(0,len(DNA),5):
if((len(DNA)%5==0) or (i < (len(DNA)-(len(DNA)%5)))):
body = body_intro + DNA[i] + body_keywords + DNA[i] + body_groupName + DNA[i+1] + body_keywords + DNA[i+1] + body_groupName + DNA[i+2] + body_keywords + DNA[i+2] + body_groupName + DNA[i+3] + body_keywords + DNA[i+3] + body_groupName + DNA[i+4] + body_keywords + DNA[i+4] + body_last
elif(len(DNA)%5==4):
body = body_intro + DNA[i] + body_keywords + DNA[i] + body_groupName + DNA[i+1] + body_keywords + DNA[i+1] + body_groupName + DNA[i+2] + body_keywords + DNA[i+2] + body_groupName + DNA[i+3] + body_keywords + DNA[i+3] + body_last
elif(len(DNA)%5==3):
body = body_intro + DNA[i] + body_keywords + DNA[i] + body_groupName + DNA[i+1] + body_keywords + DNA[i+1] + body_groupName + DNA[i+2] + body_keywords + DNA[i+2] + body_last
elif(len(DNA)%5==2):
body = body_intro + DNA[i] + body_keywords + DNA[i] + body_groupName + DNA[i+1] + body_keywords + DNA[i+1] + body_last
else:
body = body_intro + DNA[i] + body_keywords + DNA[i] + body_last