【问题标题】:Twitter Sentiment Analysis CSV io.unsupportedOperation errorTwitter 情绪分析 CSV io.unsupportedOperation 错误
【发布时间】:2017-01-11 21:28:40
【问题描述】:

我正在用 Python 编写一个程序,它使用 Twitter 的 API,它正在 twitter 上搜索关于我们想要的任何主题的推文列表,然后分析每条推文以查看它的情绪是积极还是消极。 一开始我使用 for 循环打印出 cmd 中的推文。程序中循环的初始代码

api = tweepy.API(auth)
public_tweets = api.search("Estonia")
for tweet in public_tweets:
print(tweet.text.encode("utf-8"))
analysis = TextBlob(tweet.text)
print(analysis.sentiment)

现在我想使用 csv 直接写入文本文件,但出现错误 代码:

api = tweepy.API(auth)
public_tweets = api.search("Estonia")
with  open("sentiment.txt") as scoreFile:
scoreFileWriter = csv.writer(scoreFile)
for tweet in public_tweets:
text = tweet.text
analysis = TextBlob(tweet.text)
sentiment = analysis.sentiment.polarity
scoreFileWriter.writerow([text, sentiment])

错误

C:\Users\ArturErik\Desktop>python sentiment.py
Traceback (most recent call last):
  File "sentiment.py", line 23, in <module>
    scoreFileWriter.writerow([text, sentiment])
io.UnsupportedOperation: not writable

我是 Python 新手,无法修复错误。

【问题讨论】:

  • 写入文件失败的原因有很多。文件是否存在? Python 对文件有写权限吗?从错误的外观来看,该文件似乎不是可写的。以后,请附上您为解决问题所采取的步骤。在这种情况下,您是否尝试过使文件可写?

标签: python csv twitter


【解决方案1】:

您是否尝试过使用

 open("sentiment.txt",'w') 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-20
    • 2018-04-10
    • 2016-03-15
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    • 2012-01-09
    • 2016-10-04
    相关资源
    最近更新 更多