我不知道为什么我不能将此document
写入文件:
document=['', 'feeling frisky cool cat hits join us', 'askjoe app add music videos free today saintkittsandnevis', 'give dog flea bath midnight greeeeat smells like dawn', 'cat finds flash drive spent weeks looking', 'downside cat dude means can recognize nice smell pee second', 'louis pine apple make life brighter like star please follow me', 'gonna need nice cup coffee morning', 'gonna need nice cup coffee morning', 'iphone gives warning dies smh']
以下是写入文件的代码:
^{pr2}$我得到的错误是:
Traceback (most recent call last):
File "test.py", line 85, in <module>
cleaned_tweet_file.write(document)
TypeError: expected a character buffer object
解决方案如下,添加
str
:方法^{} 接受} 的原因,并将其包装在一个}的字符串表示,而不是列表中的文档本身。在
str
而不是list
对象作为参数。这就是您的^{str()
调用中,只需就可以得到list
对象{通过使用:
您将把
^{pr2}$list
对象作为字符串写入到文件中,因此有一个大字符串:会出席的。逻辑上不是你想要的。在
您可以使用与字符串的}与{}循环结合使用的
list
一起工作的list
和for
结合使用的list
或{list
转换:使用
writelines()
:这里不需要循环或手动转换。在
使用
write
和一个简单的for
循环:这也有同样的效果。它的主要优点是它允许您每行指定额外的内容;如果您需要为每条tweet添加新行和前缀,可以显式地添加它
cleaned_tweet_file.write("Tweet: " + tweet + "\n")
。在相关问题 更多 >
编程相关推荐