导出/导入使用 python anytree 2.4.3 库创建的树 [英] Exporting / Importing trees created with python anytree 2.4.3 library

查看:78
本文介绍了导出/导入使用 python anytree 2.4.3 库创建的树的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 anytree 库创建了一棵树.我希望能够修改它,然后导出 - 将其保存到磁盘,然后将修改后的内容导入回来.例如,示例树:

I create a tree with anytree library. I want to be able to modify it, then export - save it to disk, and import it back with the modifications. For instance, the example tree:

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)

Udo
├── Marc
│   └── Lian
└── Dan
    ├── Jet
    ├── Jan
    └── Joe

我可以修改它,例如切断丹并为马克添加一个孩子

I can modify it, for instance cutting Dan off and adding a children to Marc

dan.parent = None 
bonny = Node ("Bonny", parent = marc)

Udo
└── Marc
    ├── Lian
    └── Bonny

但是当我将树导出到 json 然后将其导入回来时,我似乎能够引用的唯一节点是根.所以我不能再做这种修改了,因为像 danmarc 这样的变量名不存在,也就是说,我找不到引用节点的方法.我错过了什么吗?

But when I export the tree to json and then import it back, the only node I seem to be able to reference is the root. So I can't do this kind of modification anymore because the variable names like dan or marc are not there, ie, I don't find a way to reference a node. Am I missing anything?

with open ('cajon/anytreeexample.json', 'r+', encoding = 'utf-8') as f:
    datos = importer.read(f)

print (datos)

这意味着在导入树之后,您拥有的只是一个根节点

This means that after importing the tree what you have is just a root node

AnyNode(name='Udo')  # Udo is the root

从这里你可以得到Udo的孩子,孩子们的孩子喜欢

From here you can get Udo's children, and the children's children like

marc, dan = udo.children
lian = marc.children
jet, jan, joe = dan.children

但它们不是作为节点工作

But they are not working as a node

print (lian.parent)
AttributeError: 'tuple' object has no attribute 'parent'

而且似乎您不能将孩子附加到他们身上,这是我使用这种结构的主要目的:

And seems you cannot attach a children to them, which is my main purpose with this structure:

sonny = AnyNode("Sonny", parent = lian)
TypeError: __init__() got multiple values for argument 'parent'

所以我的问题是,有没有办法将 json 保存的树加载到适当的 anytree 结构中,您可以在其中附加新节点?

So my question is, is there a way to load the json saved tree into a proper anytree structure, where you can append new nodes?

推荐答案

实际上你做对了:你只是忘记了一个 ,

You actually did it the right way: you just forgot a ,

from anytree import Node

udo = Node("Udo")
marc = Node("Marc", parent=udo)
Node("Lian", parent=marc)

lian, = marc.children # this is a tupel, even if its only one entry -> add ,
sonny = Node("Sonny", parent = lian)

print (lian.parent)
> Node('/Udo/Marc')

print (sonny)
> Node('/Udo/Marc/Lian/Sonny')

<小时>

@How to find your nodes: 你正在寻找 anytrees find_by_attr:

搜索属性名称为 [...] 的单个节点

Search for a single node with attribute name having value [...]

所以在加载你的树之后

with open ('cajon/anytreeexample.json', 'r+', encoding = 'utf-8') as f:
    datos = importer.read(f)

您可以按名称搜索节点:

You can search for nodes by name:

udo = datos.find_by_attr("Udo") # should be the same as datos if udo was the root

然后像这样添加更多:

Node("Sonny", parent = datos.find_by_attr("lian"))

这篇关于导出/导入使用 python anytree 2.4.3 库创建的树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆