Map template:
TELEFONIA
+ CELULAR
+ CORRETOR
+ a
~ CANCELAR LINHA
~ SOLICITAR LINHA
~ b
+ aba
~ CANCELAMENTO DE LINHA
~ CANCELAMENTO DE MODEM
+ CMS
~ ALTERAR
JSON template:
{
"label" : "TELEFONIA",
"valor":[
{"label" : "CELULAR", "valor":[
{"label" : "CORRETOR", "valor":[
{"label" : "a", "valor":[
{"label" : "CANCELAR LINHA" },
{"label" : "SOLICITAR LINHA" },
]}
]},
{"label" : "b" },
{"label" : "aba", "valor":[
{"label" : "CANCELAMENTO DE LINHA" },
{"label" : "CANCELAMENTO DE MODEM" },
]},
]},
{"label" : "CMS", "valor" :[
{"label" : "ALTERAR" },
]}
]
}
A code I made (But I've deleted a lot of it already):
text = 'TELEFONIA\n\t+ CELULAR\n\t\t+ CORRETOR\n\t\t\t+ a\n\t\t\t\t~ CANCELAR LINHA\n\t\t\t\t~ SOLICITAR LINHA\n\t\t~ b\n\t\t+ aba\n\t\t\t~ CANCELAMENTO DE LINHA\n\t\t\t~ CANCELAMENTO DE MODEM\n\t+ CMS\n\t\t~ ALTERAR'
nivelAnterior = 0
def createTree(text):
e = []
for line in text.split("\n"):
# valor = (line.split("~ ")[1] if nivelAnterior == line.count('\t'))
if "+" in line:
valor = line.split("+ ")[1]
elif "~" in line:
valor = line.split("~ ")[1]
else:
valor = line
e.append({"nivel":line.count('\t'),"valor": valor})
return e
def a(val):
pai = 0
prev = 0
listOfChild = []
listOfObjs = []
valuePai = ""
i = 0
for value in val:
nivel = value["nivel"]
if nivel > prev:
# child
pai = prev
elif nivel == prev:
# bro
pai = pai
elif nivel < prev:
# parent
pai += nivel - prev
prev = nivel
print("\nvalor atual: ",value["valor"])
print("\t",listOfChild)
print('\n\t',listOfObjs)
val = createTree(text)
# print(val)
a(val)