Creating a JSON object from a tabbed text file

I have this sample text file: (I don't know what you call it, Tree?)

key1
    subkey1
    subkey2
        choice1
key2
    subkey1
    subkey2

      

I want it to look like this:

[
    {   
        "text":"key1",
        "children":[
            {   
                "text":"subkey1",
                children:[]
            },
            {
                "text":"subkey2",
                children:[
                    {
                        "text":"choice1",
                        "children":[]
                    }
                ]
            },
        ]
    },
    {   
        "text":"key2",
        "children":[
            {   
                "text":"subkey1",
                children:[]
            },
            {   
                "text":"subkey2",
                children:[]
            },
        ]
    }
]

      

This is what I am doing, I do not understand how you get the children in the parent and it should be infinitely deep possible.

import itertools
def r(f, depth, parent, l, children):
    for line in f:
        line = line.rstrip()
        newDepth = sum(1 for i in itertools.takewhile(lambda c: c=='\t', line))
        node = line.strip()
        if parent is not None:
            print parent, children
            children = [{"txt":node, "children":[]}]
            # l.append({"txt":parent, "children":children})
        r(f, newDepth, node, l, children)
json_list = []
r(open("test.txt"), 0, None, json_list, [])
print json_list

      

+3


source to share


1 answer


First rule, avoid recursion if you can ... Here you only need to know the ancestors and they can be easily stored in a list. Note that depth

0 is reserved for the root node, and the first is "custom" depth 1

, so +1

when counting tabs.



f = open("/tmp/test.txt", "r")

depth = 0
root = { "txt": "root", "children": [] }
parents = []
node = root
for line in f:
    line = line.rstrip()
    newDepth = len(line) - len(line.lstrip("\t")) + 1
    print newDepth, line
    # if the new depth is shallower than previous, we need to remove items from the list
    if newDepth < depth:
        parents = parents[:newDepth]
    # if the new depth is deeper, we need to add our previous node
    elif newDepth == depth + 1:
        parents.append(node)
    # levels skipped, not possible
    elif newDepth > depth + 1:
        raise Exception("Invalid file")
    depth = newDepth

    # create the new node
    node = {"txt": line.strip(), "children":[]}
    # add the new node into its parent children
    parents[-1]["children"].append(node)

json_list = root["children"]
print json_list

      

+5


source







All Articles