Skip to content Skip to sidebar Skip to footer

How To Specify Childrens In Anytree And Print A Tree

I am trying to print a tree using Wikipedia's sections but I am not able to figure out how to specify children node in anytree. Here is what I have tried so far, import wikipediaap

Solution 1:

I think you need to double-check the docs and work through an example or two in the anytree class. This class works with its self-defined tree structure, but sections is a straightforward list, not suitable to present to RenderTree. I checked your interfacing with some simple print commands:

sections =  main_page.sections
print(type(sections), len(sections))
print("\n------------ sections -----------\n", sections)
render = RenderTree(sections)
print(type(render))
print("\n------------ final print -----------\n")
print(render)
print("\n------------ final print done -----------\n")

Output:

<class 'list'> 7

------------ sections -----------
 [Section: History (1):
The website was created
...
]
<class 'anytree.render.RenderTree'>

------------ final print -----------

Traceback (most recent call last):
...

Your list input does not have the Node structure that anytree expects.


Post a Comment for "How To Specify Childrens In Anytree And Print A Tree"