【问题标题】:How can I extract all satellite adjectives from WordNet NLTK and save them to a text file?如何从 WordNet NLTK 中提取所有附属形容词并将它们保存到文本文件中?
【发布时间】:2015-06-29 19:35:58
【问题描述】:

我正在尝试从 WordNet 中提取所有附属形容词同义词集并将它们保存到文本文件中。请注意,卫星形容词在同义词集名称中表示为“s”,例如“(fantastic.s.02)”。以下是我的代码:

def extract_sat_adjectives():
    sat_adj_counter = 0
    sat_adjectives = []
    for i in wn.all_synsets():
        if i.pos() in ['s']:
            sat_adj_counter +=1
            sat_adjectives = sat_adjectives + [i.name()]
    fo = open("C:\\Users\\Nora\\Desktop\\satellite_adjectives.txt", "wb")
    for x in sat_adjectives:
        fo.write("%s\n" % x)
    fo.close()


extract_sat_adjectives()

我得到的错误是:

TypeError: 'str' does not support the buffer interface  

如何将形容词保存到文本文件中?提前致谢。

【问题讨论】:

  • 你能提供整个 Traceback 吗?哪一行导致 TypeError?
  • 我当然可以:消息文件名行位置回溯 C:\Users\Nora\Documents\module1.py 37 extract_sat_adjectives C:\Users\Nora\Documents\module1.py 32 TypeError : 'str' 不支持缓冲接口
  • 你知道是哪一行触发了异常吗?
  • 没有指定行。我认为这是因为列表中的项目不是字符串,因此无法写入文本文件..但我不确定..
  • 我们在询问源代码中的这一行。第 32 行是哪一行?

标签: python nltk wordnet


【解决方案1】:

该错误与编码错误和str()的组合有关

for x in sat_adjectives:
    fo.write("%s\n" % x)

改为:

for x in sat_adjectives:
    fo.write(bytes("%s\n" % x, 'UTF-8'))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    相关资源
    最近更新 更多