-1

I am trying to convert an indented text to a list of nested dict in python inspired by this thread Creating a tree/deeply nested dict from an indented text file in python It helped me start but I am still failing short to accomplishing the desired result

indented_text = """
# Level 1
## Level 2
### Level 3
#### Level 4
##### Level 5
###### Level 6
##### Level 5
#### Level 4
##### Level 5
###### Level 6
### Level 3
#### Level 4
## Level 2
### Level 3
#### Level 4
#### Level 4
### Level 3
"""
class Node:
    def __init__(self, indented_line):
        # self.t = t
        # self.d = d
        # self.p = {}
        # self.v = v ---------
        # self.c = [] ------
        self.t = 'list_item'
        self.d = indented_line.index('# ') # len(indented_line) - len(indented_line.lstrip())
        self.p = {}
        self.v = indented_line[self.d + 1:].strip()
        self.c = []

    def add_children(self, nodes):
        childlevel = nodes[0].d
        while nodes:
            node = nodes.pop(0)
            if node.d == childlevel: # add node as a child
                self.c.append(node)
            elif node.d > childlevel: # add nodes as grandchildren of the last child
                nodes.insert(0,node)
                self.c[-1].add_children(nodes)
            elif node.d <= self.d: # this node is a sibling, no more children
                nodes.insert(0,node)
                return

root = Node('# root')
root.add_children([Node(line) for line in indented_text.splitlines() if line.strip()])

now I need the output to be

{
    "t": "heading",
    "d": 1,
    "p": {},
    "v": "Level 1",
    "c": [
        {
            "t": "heading",
            "d": 2,
            "p": {},
            "v": "Level 2",
            "c": [
                {
                    "t": "heading",
                    "d": 3,
                    "p": {},
                    "v": "Level 3",
                    "c": [
                        {
                            "t": "heading",
                            "d": 4,
                            "p": {},
                            "v": "Level 4",
                            "c": [
                                {
                                    "t": "heading",
                                    "d": 5,
                                    "p": {},
                                    "v": "Level 5",
                                    "c": [
                                        {
                                            "t": "heading",
                                            "d": 6,
                                            "p": {},
                                            "v": "Level 6"
                                        }
                                    ]
                                },
                                {
                                    "t": "heading",
                                    "d": 5,
                                    "p": {},
                                    "v": "Level 5"
                                }
                            ]
                        },
                        {
                            "t": "heading",
                            "d": 4,
                            "p": {},
                            "v": "Level 4",
                            "c": [
                                {
                                    "t": "heading",
                                    "d": 5,
                                    "p": {},
                                    "v": "Level 5",
                                    "c": [
                                        {
                                            "t": "heading",
                                            "d": 6,
                                            "p": {},
                                            "v": "Level 6"
                                        }
                                    ]
                                }
                            ]
                        }
                    ]
                },
                {
                    "t": "heading",
                    "d": 3,
                    "p": {},
                    "v": "Level 3",
                    "c": [
                        {
                            "t": "heading",
                            "d": 4,
                            "p": {},
                            "v": "Level 4"
                        }
                    ]
                }
            ]
        },
        {
            "t": "heading",
            "d": 2,
            "p": {},
            "v": "Level 2",
            "c": [
                {
                    "t": "heading",
                    "d": 3,
                    "p": {},
                    "v": "Level 3",
                    "c": [
                        {
                            "t": "heading",
                            "d": 4,
                            "p": {},
                            "v": "Level 4"
                        },
                        {
                            "t": "heading",
                            "d": 4,
                            "p": {},
                            "v": "Level 4"
                        }
                    ]
                },
                {
                    "t": "heading",
                    "d": 3,
                    "p": {},
                    "v": "Level 3"
                }
            ]
        }
    ]
}

I am unable to finish and get the output as required...

bherbruck
  • 1,821
  • 1
  • 4
  • 14
  • 1
    What output do you get? How does it differ from what you expect? What debugging have you tried to fix the problem? Can you condense your code down to a [MRE] and format your expected output so that it is readable? – Pranav Hosangadi Feb 15 '21 at 17:45
  • why/what are all those manual c, v, t, d, p you added? – bherbruck Feb 15 '21 at 18:47
  • @PranavHosangadi the output is my issue I I don't know how to convert that to printable dict / json string – EternalNewB Feb 15 '21 at 19:30
  • @TenaciousB I am not sure what u mean, this is the format I need the output with – EternalNewB Feb 15 '21 at 19:31
  • Did you mean to ask [how to serialize a class to JSON](https://stackoverflow.com/questions/10252010/serializing-class-instance-to-json)? – Pranav Hosangadi Feb 15 '21 at 19:33
  • @PranavHosangadi pretty much but the example u linked too is "flat" so easy to do with a .__dict__ etc... I am unable to get the output I need from my code. thank you – EternalNewB Feb 15 '21 at 19:35

1 Answers1

1

I was able to solve my problem by using

json.dumps(root, default=lambda v: v.__dict__)

thank you all for chiming in and https://stackoverflow.com/users/843953/pranav-hosangadi for pointing me in the right direction