Skip to content

Commit

Permalink
Merge pull request #98 from Bernardoow/save2file
Browse files Browse the repository at this point in the history
Added example about save2file
  • Loading branch information
caesar0301 committed Jul 8, 2018
2 parents bbd7bc5 + 72e7099 commit 5b7ed53
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions docs/source/examples.rst
Expand Up @@ -136,6 +136,14 @@ the data field is appended into JSON string. For example,
>>> print(tree.to_json(with_data=True))
{"Harry": {"data": null, "children": [{"Bill": {"data": null}}, {"Jane": {"data": null, "children": [{"Diane": {"data": null}}, {"Mark": {"data": null}}]}}, {"Mary": {"data": null}}]}}
**Example 10**: Save tree into file
The function save2file require a filename.
The file is opened to write using mode 'ab'.
.. code-block:: sh
>>> tree.save2file('tree.txt')
Advanced Usage
----------------
Expand Down
12 changes: 12 additions & 0 deletions examples/save_tree_2_file.py
@@ -0,0 +1,12 @@
# -*- coding: utf-8 -*-

from treelib import Tree

tree = Tree()
tree.create_node("Harry", "harry") # root node
tree.create_node("Jane", "jane", parent="harry")
tree.create_node("Bill", "bill", parent="harry")
tree.create_node("Diane", "diane", parent="jane")
tree.create_node("Mary", "mary", parent="diane")
tree.create_node("Mark", "mark", parent="jane")
tree.save2file('tree.txt')

0 comments on commit 5b7ed53

Please sign in to comment.