【发布时间】:2016-02-27 08:11:31
【问题描述】:
for subtree3 in tree.subtrees():
if subtree3.label() == 'CLAUSE':
print(subtree3)
print subtree3.leaves()
使用此代码,我能够提取树的叶子。哪个是:
[('talking', 'VBG'), ('constantly', 'RB')] 举个例子。这是完全正确的。现在我希望将此 Tree 元素转换为字符串或列表以进行进一步处理。我该怎么做?
我尝试了什么
for subtree3 in tree.subtrees():
if subtree3.label() == 'CLAUSE':
print(subtree3)
print subtree3.leaves()
fo.write(subtree3.leaves())
fo.close()
但它会引发错误:
Traceback (most recent call last):
File "C:\Python27\Association_verb_adverb.py", line 35, in <module>
fo.write(subtree3.leaves())
TypeError: expected a character buffer object
我只想将叶子存储在一个文本文件中。
【问题讨论】:
-
您的输入是什么,您希望输出什么?能给我举个例子吗?在最顶层的子树中也可以有多个层,因此根据您需要的输出,您遍历树以将其打印出来的方式会有所不同
标签: python list tree tuples nltk