I recommend anytree (I am the author).
Example:
from anytree import Node, RenderTree
udo = Node("Udo")
marc = Node("Marc", parent=udo)
lian = Node("Lian", parent=marc)
dan = Node("Dan", parent=udo)
jet = Node("Jet", parent=dan)
jan = Node("Jan", parent=dan)
joe = Node("Joe", parent=dan)
print(udo)
Node('/Udo')
print(joe)
Node('/Udo/Dan/Joe')
for pre, fill, node in RenderTree(udo):
print("%s%s" % (pre, node.name))
Udo
├── Marc
│ └── Lian
└── Dan
├── Jet
├── Jan
└── Joe
print(dan.children)
(Node('/Udo/Dan/Jet'), Node('/Udo/Dan/Jan'), Node('/Udo/Dan/Joe'))
anytree has also a powerful API with:
- simple tree creation
- simple tree modification
- pre-order tree iteration
- post-order tree iteration
- resolve relative and absolute node paths
- walking from one node to an other.
- tree rendering (see example above)
- node attach/detach hookups
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…