您可以使用第三方python包,称为anytree表单
https://pypi.org/project/anytree/
pip install anytree
代码.py
from anytree import Node, RenderTree
A = Node("A")
A1 = Node ("A1", parent=A)
A11 = Node("A11", parent = A1)
A12 = Node("A12", parent = A1)
A121 = Node("A121", parent = A12)
A122 = Node("A122", parent = A12)
A2 = Node("A2", parent=A1)
B = Node("B")
C = Node("C")
C1 = Node("C1", parent=C)
C2 = Node("C2", parent=C)
print (A)
print(A122)
for pre, fill, node in RenderTree(A):
print("%s%s" % (pre, node.name))
产量
============= RESTART: C:/Python36/MyTestScripts/anytree_test.py =============
Node('/A')
Node('/A/A1/A12/A122')
A
âââ A1
âââ A11
âââ A12
â âââ A121
â âââ A122
âââ A2
>>>