[英]TypeError: must be str, not list
the problem is output result is not save in csv file. 问题是输出结果未保存在csv文件中。 I'm using this code to weight-age the words positive and negative.I want to save in the csv file.Firstly, read the csv file ,apply tf-idf and output display on shell,but error disply when result write in csv file.
我正在使用此代码对正负两个单词进行加权处理。我想保存在csv文件中。首先,读取csv文件,应用tf-idf并在shell上显示输出,但是将结果写入csv时错误显示文件。
for i, blob in enumerate(bloblist):
print("Top words in document {}".format(i + 1))
scores = {word: tfidf(word, blob, bloblist) for word in blob.words}
sorted_words = sorted(scores.items(), reverse=True)
print(sorted_words)
final = open("tfidf.csv", "w").write(sorted_words)
print(final)
print("done")
The error is: 错误是:
Top words in document 1
Traceback (most recent call last):
File "C:\Python34\webcrawler-Final.py", line 38, in <module>
final = open("tfidf.csv", "w").write(sorted_words)
TypeError: must be str, not list
试试这个。
sorted_words = ''.join(sorted(scores.items(), reverse=True))
As you dont specify in your post, I dont know which is the separator between values of the tuple, so I added a '\\n'
. 正如您未在帖子中指定的那样,我不知道哪个是元组的值之间的分隔符,因此我添加了一个
'\\n'
。 You can change that to ' '
or whatever you want. 您可以将其更改为
' '
或任何您想要的名称。
final = open("tfidf.csv", "w").write('\\n'.join('%s, %s' % x for x in sorted_words))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.