简体   繁体   English

如何将结果从nltk函数“ most_informative_features”保存到Python中的txt文件中

[英]How to save the results from nltk function “most_informative_features” to a txt file in Python

Sorry if this is a noobie question! 抱歉,这是一个noobie问题! I am carrying out sentiment analysis on python using nltk. 我正在使用nltk对python进行情感分析。 It has a function which returns the most informative features but whenever i try and save the results to a text file, i get the following error 'TypeError: must be str, not list'. 它具有返回大多数信息的功能,但是每当我尝试将结果保存到文本文件时,都会收到以下错误“ TypeError:必须为str,而不是list”。 the code i am using is as follows 我正在使用的代码如下

classifier.most_informative_features(100)  

str(information)
saveFile = open('informationFile.txt', 'w')

saveFile.write(information)
saveFile.close()

any ideas what i am doing wrong? 任何想法我在做什么错?

You need to assign the conversion from list to string to something, or do in place... 您需要将从列表到字符串的转换分配给某些内容,或者就地执行...

saveFile.write(''.join(information))

Applying str to a variable generates a value, but doesn't change the variable (unless you assign it) str应用于变量会生成一个值,但不会更改该变量(除非您为其分配值)

>>> bar
['a', 'b', 'c']
>>> str(bar)
"['a', 'b', 'c']"
>>> bar
['a', 'b', 'c']
>>> ', '.join(bar)
'a, b, c'
>>> bar
['a', 'b', 'c']
>>> bar = ', '.join(bar)
>>> bar
'a, b, c'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM