我试图从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
如何将形容词保存到文本文件中?提前致谢。
答案 0 :(得分:2)
错误与编码错误和str()
for x in sat_adjectives:
fo.write("%s\n" % x)
更改为:
for x in sat_adjectives:
fo.write(bytes("%s\n" % x, 'UTF-8'))