TypeError:必须为str,而不是list [英] TypeError: must be str, not list

查看:151
本文介绍了TypeError:必须为str,而不是list的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是输出结果未保存在csv文件中. 我正在使用此代码对正负两个单词进行加权处理.我想保存在csv文件中.首先,读取csv文件,应用tf-idf并在shell上输出显示,但是将结果写入csv时错误显示文件.

the problem is output result is not save in csv file. 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.

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")

错误是:

   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))

这篇关于TypeError:必须为str,而不是list的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆