图片的Anytree字典节点(png)

时间:2019-03-05 22:09:36

标签: python anytree

我正在使用任何树python模块。我将Dict存储在树的每个节点中,并希望将树转换为图片时出现错误。

树:

{'man': False, 'goat': False, 'lion': False, 'grass': False, 'state': True}
├── {'man': True, 'goat': False, 'lion': False, 'grass': False, 'state': False}
├── {'man': True, 'goat': False, 'lion': True, 'grass': False, 'state': False}
├── {'man': True, 'goat': True, 'lion': False, 'grass': False, 'state': True}
│   ├── {'man': False, 'goat': True, 'lion': False, 'grass': False, 'state': True}
│   │   ├── {'man': True, 'goat': True, 'lion': False, 'grass': False, 'state': 'repeat'}
│   │   ├── {'man': True, 'goat': True, 'lion': True, 'grass': False, 'state': True}
│   │   │   ├── {'man': False, 'goat': True, 'lion': True, 'grass': False, 'state': False}
│   │   │   ├── {'man': False, 'goat': True, 'lion': False, 'grass': False, 'state': 'repeat'}
│   │   │   └── {'man': False, 'goat': False, 'lion': True, 'grass': False, 'state': True}
│   │   │       ├── {'man': True, 'goat': False, 'lion': True, 'grass': False, 'state': False}
│   │   │       ├── {'man': True, 'goat': True, 'lion': True, 'grass': False, 'state': 'repeat'}
│   │   │       └── {'man': True, 'goat': False, 'lion': True, 'grass': True, 'state': True}
│   │   │           ├── {'man': False, 'goat': False, 'lion': True, 'grass': True, 'state': True}
│   │   │           │   ├── {'man': True, 'goat': False, 'lion': True, 'grass': True, 'state': 'repeat'}
│   │   │           │   └── {'man': True, 'goat': True, 'lion': True, 'grass': True, 'state': 'Goal'}
│   │   │           ├── {'man': False, 'goat': False, 'lion': False, 'grass': True, 'state': 'repeat'}
│   │   │           └── {'man': False, 'goat': False, 'lion': True, 'grass': False, 'state': 'repeat'}
│   │   └── {'man': True, 'goat': True, 'lion': False, 'grass': True, 'state': True}
│   │       ├── {'man': False, 'goat': True, 'lion': False, 'grass': True, 'state': False}
│   │       ├── {'man': False, 'goat': False, 'lion': False, 'grass': True, 'state': True}
│   │       │   ├── {'man': True, 'goat': False, 'lion': False, 'grass': True, 'state': False}
│   │       │   ├── {'man': True, 'goat': False, 'lion': True, 'grass': True, 'state': 'repeat'}
│   │       │   └── {'man': True, 'goat': True, 'lion': False, 'grass': True, 'state': 'repeat'}
│   │       └── {'man': False, 'goat': True, 'lion': False, 'grass': False, 'state': 'repeat'}
│   └── {'man': False, 'goat': False, 'lion': False, 'grass': False, 'state': 'repeat'}
└── {'man': True, 'goat': False, 'lion': False, 'grass': True, 'state': False}

转换为Png:

DotExporter(udo).to_dotfile('udo.dot')

from graphviz import Source
Source.from_file('udo.dot')

错误:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-23-1a4e66c099be> in <module>()
----> 1 DotExporter(udo).to_dotfile('udo.dot')
      2 
      3 from graphviz import Source
      4 Source.from_file('udo.dot')

/opt/conda/lib/python3.6/site-packages/anytree/exporter/dotexporter.py in to_dotfile(self, filename)
    212         """
    213         with codecs.open(filename, "w", "utf-8") as file:
--> 214             for line in self:
    215                 file.write("%s\n" % line)
    216 

/opt/conda/lib/python3.6/site-packages/anytree/exporter/dotexporter.py in __iter(self, indent, nodenamefunc, nodeattrfunc, edgeattrfunc, edgetypefunc)
    158         for option in self.__iter_options(indent):
    159             yield option
--> 160         for node in self.__iter_nodes(indent, nodenamefunc, nodeattrfunc):
    161             yield node
    162         for edge in self.__iter_edges(indent, nodenamefunc, edgeattrfunc, edgetypefunc):

/opt/conda/lib/python3.6/site-packages/anytree/exporter/dotexporter.py in __iter_nodes(self, indent, nodenamefunc, nodeattrfunc)
    175             nodeattr = nodeattrfunc(node)
    176             nodeattr = " [%s]" % nodeattr if nodeattr is not None else ""
--> 177             yield '%s"%s"%s;' % (indent, DotExporter.esc(nodename), nodeattr)
    178 
    179     def __iter_edges(self, indent, nodenamefunc, edgeattrfunc, edgetypefunc):

/opt/conda/lib/python3.6/site-packages/anytree/exporter/dotexporter.py in esc(str)
    240     def esc(str):
    241         """Escape Strings."""
--> 242         return str.replace("\"", "\\\"")

AttributeError: 'dict' object has no attribute 'replace'

我阅读了任何树的全部文档,但从树中找不到任何内容,我也尝试了google,但没有发现任何帮助。由于在任何树上所做的工作都很少。所以如何将字典树转换成图片,请给我一些建议。

0 个答案:

没有答案