本文整理汇总了Python中treelib.Tree.save2file方法的典型用法代码示例。如果您正苦于以下问题:Python Tree.save2file方法的具体用法?Python Tree.save2file怎么用?Python Tree.save2file使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类treelib.Tree
的用法示例。
在下文中一共展示了Tree.save2file方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: print_prob_val
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import save2file [as 别名]
def print_prob_val(self, fname="OutTree.txt"):
n_tree = Tree(tree=self.tree)
for node in n_tree.nodes:
node = n_tree.get_node(node)
node.tag = "{tag} - {val} - {prob}".format(tag=node.tag, val=node.data[0], prob=node.data[1])
n_tree.save2file(fname)
self.tree = None
示例2: TreePipeline
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import save2file [as 别名]
class TreePipeline(object):
def open_spider(self, spider):
self.tree = Tree()
self.tree.create_node("root", "root")
def process_item(self, item, spider):
lst = item['text']
lst = [x.strip() for x in [y.replace('...', '') for y in lst]]
item['pagetitle'] = item['pagetitle'].replace('...', '')
lst[-1] = item['pagetitle']
for idx, elem in enumerate(lst):
if idx == 0:
previous = "root"
else:
previous = "|".join(lst[:idx])
elem = "|".join(lst[:idx + 1])
# elem = elem.replace('...', '')
elem = elem.encode('utf-8').decode('utf-8')
if not self.tree.contains(elem):
print "Adding node %s" % elem
self.tree.create_node(elem, elem, parent=previous)
# self.tree.show()
return item
def close_spider(self, spider):
self.tree.show()
with open(makepath('data/cats/tree.json'), 'w') as outfile:
outfile.write(self.tree.to_json())
self.tree.save2file(makepath('data/cats/tree.tree'))
示例3: Tree
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import save2file [as 别名]
## Create the family 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("George", "george", parent="diane")
tree.create_node("Mary", "mary", parent="diane")
tree.create_node("Jill", "jill", parent="george")
tree.create_node("Mark", "mark", parent="jane")
print ("#" * 4 + "Breakdown of out family")
tree.show(cmp=lambda x, y: cmp(x.tag, y.tag), key=None, reverse=True)
# tree.show(key=lambda x: x.tag, reverse=False)
tree.save2file("/home/chenxm/Desktop/tree.txt", idhidden=False)
print ("\n")
print ("#" * 4 + "All family members in DEPTH mode")
for node in tree.expand_tree(mode=Tree.ZIGZAG):
print tree[node].tag
print ("\n")
print ("#" * 4 + "All family members without Diane sub-family")
tree.show(idhidden=False, filter=lambda x: x.identifier != "diane")
# for node in tree.expand_tree(filter=lambda x: x.identifier != 'diane', mode=Tree.DEPTH):
# print tree[node].tag
print ("\n")
示例4: Tree
# 需要导入模块: from treelib import Tree [as 别名]
# 或者: from treelib.Tree import save2file [as 别名]
# -*- 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')