如何在python中的txt文件中保存解析树

时间:2019-03-06 07:30:02

标签: python parsing nltk

每当尝试保存由我指定的一组规则生成的解析树时,我都会遇到问题。我想要将该解析树保存在一个新的文本文件中。当我这样做时,它将创建一个新的文本文件,但它为空,并且我的代码给出了错误

  

'charmap'编解码器无法编码位置12-18中的字符:character   映射到

这是我的代码

sen = "Heyy Jack whats up"
sent = word_tokenize(sen)
gram = ("""
S -> NP VP
NP -> NU | N N
VP -> NP NP
""")
grammar1 = nltk.cfg.fromstring(gram)
sr_parser = nltk.RecursiveDescentParser(grammar1)
for tree in sr_parser.parse(sent):    
    print(tree)
    values = tree
    with open("new.txt", "w") as output: ## creates new file but empty
        output.write(str(values))

1 个答案:

答案 0 :(得分:0)

尝试使用utf-8编码:

with open("new.txt", "w", encoding='utf-8') as output: ## creates new file but empty
        output.write(str(values))

您还可以将io用于python 2向后兼容:

import io
with io.open("new.txt", "w", encoding='utf-8') as output: ## creates new file but empty
    output.write(str(values))