{"id":4823,"date":"2024-09-13T16:56:21","date_gmt":"2024-09-13T08:56:21","guid":{"rendered":"http:\/\/cnliutz.ipyingshe.net\/?p=4823"},"modified":"2024-09-13T18:44:09","modified_gmt":"2024-09-13T10:44:09","slug":"python%e7%9a%84anytree-%e5%92%8c-prettytree-%e4%b8%a4%e4%b8%aa%e5%ba%93%e6%9d%a5%e5%a4%84%e7%90%86%e5%92%8c%e5%b1%95%e7%a4%ba%e6%a0%91%e5%bd%a2%e7%bb%93%e6%9e%84","status":"publish","type":"post","link":"http:\/\/cnliutz.wicp.vip\/?p=4823","title":{"rendered":"Python\u7684anytree\u00a0\u548c\u00a0prettytree\u00a0\u4e24\u4e2a\u5e93\u6765\u5904\u7406\u548c\u5c55\u793a\u6811\u5f62\u7ed3\u6784"},"content":{"rendered":"\n<p>\u8981\u4f7f\u7528 <code>anytree<\/code> \u548c <code>prettytree<\/code> \u4e24\u4e2a\u5e93\u6765\u5904\u7406\u548c\u5c55\u793a\u6811\u5f62\u7ed3\u6784\uff0c\u53ef\u4ee5\u53c2\u8003\u4ee5\u4e0b\u6b65\u9aa4\uff1a<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">\u4f7f\u7528 <code>anytree<\/code> \u521b\u5efa\u548c\u904d\u5386\u6811<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b89\u88c5 <code>anytree<\/code><\/strong>\uff1a <code>pip install anytree<\/code><\/li>\n\n\n\n<li><strong>\u521b\u5efa\u6811\u7ed3\u6784<\/strong>\uff1a <\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>from anytree import Node, RenderTree\n\n# \u521b\u5efa\u8282\u70b9\nroot = Node(\"Root\")\nchild1 = Node(\"Child1\", parent=root)\nchild2 = Node(\"Child2\", parent=root)\ngrandchild1 = Node(\"Grandchild1\", parent=child1)\n\n# \u6e32\u67d3\u6811\u7ed3\u6784\nfor pre, fill, node in RenderTree(root):\n    print(f\"{pre}{node.name}\")\n<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>\u5b89\u88c5 <code>prettytree<\/code><\/strong>\uff1a <code>pip install prettytree<\/code><\/li>\n\n\n\n<li><strong>\u7f8e\u5316\u6811\u7ed3\u6784<\/strong>\uff1a\uff08\u8fd9\u6bb5\u4ee3\u7801\u8fd0\u884c\u51fa\u9519\uff01\uff01\uff01\uff1f\uff1f\uff1f\uff09<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>from prettytree import PrettyTree\n\n# \u521b\u5efa PrettyTree \u5bf9\u8c61\ntree = PrettyTree(\"Root\")\ntree.add_child(\"Child1\").add_child(\"Grandchild1\")\ntree.add_child(\"Child2\")\n\n# \u6253\u5370\u7f8e\u5316\u540e\u7684\u6811\u7ed3\u6784\nprint(tree)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u8981\u4f7f\u7528 anytree \u548c prettytree \u4e24\u4e2a\u5e93\u6765\u5904\u7406\u548c\u5c55\u793a\u6811\u5f62\u7ed3\u6784\uff0c\u53ef <span class=\"readmore\"><a href=\"http:\/\/cnliutz.wicp.vip\/?p=4823\">Continue Reading<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-4823","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/4823","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4823"}],"version-history":[{"count":3,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/4823\/revisions"}],"predecessor-version":[{"id":4828,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=\/wp\/v2\/posts\/4823\/revisions\/4828"}],"wp:attachment":[{"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4823"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4823"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/cnliutz.wicp.vip\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4823"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}