How to create a JSON map based on indented file

0
Hello, I have a problem that is the following I have a map with the following format and I need to create a script that converts that map to a specific JSON template in python:

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)
    
asked by anonymous 07.12.2018 / 17:04

1 answer

1

SOLUTION:

After rethinking all logic I came up with the following solution:

map = open("MAPA CATEGORIAS.MAP",encoding="utf8").read()

def getListOfChild(listOfElements, fatherIndex):
    i = fatherIndex + 1
    ret = []
    for element in listOfElements:
        if listOfElements[fatherIndex].count("#") < listOfElements[i].count("#"):
            ret.append({"lineIndex":i,"label":listOfElements[i]})
        else:
            break;
        i += 1
    print("getListOfChild:",ret)
    return ret;
def getListOfExp(mapGeral,fatherIndex):
    expd = []
    i = fatherIndex
    father = {"fatherIndex":fatherIndex,"fatherLevel":mapGeral[fatherIndex].count("#")}
    map = getListOfChild(mapGeral, fatherIndex)
    for line in map:
        print(line["label"])
        print(("#"*(father["fatherLevel"]+1))+"+")
        if line["label"].startswith(("#"*(father["fatherLevel"]+1))+"+"):
            line["valor"] = getListOfExp(mapGeral,line["lineIndex"])
            expd.append(line)
        elif line["label"].startswith(("#"*(father["fatherLevel"]+1))+"~"):
            expd.append(line)
    return expd

map = map.split("\n")
print(map[1].count("#"))
open("result.json","w+",encoding="utf-8").write(str(getListOfExp(map,208)).replace('\'','"').replace('#','').replace('+ ','').replace('~ ',''))

ps: The code is already linked the way I needed to read and write files,

Thanks for the support,

    
08.12.2018 / 02:45