簡體   English   中英

Twitter情緒分析CSV io.unsupported操作錯誤

[英]Twitter Sentiment Analysis CSV io.unsupportedOperation error

我正在用Python編寫一個程序,該程序使用Twitter的API,並且在Twitter上搜索有關我們想要的任何主題的推文列表,然后分析每條推文以查看其情緒是正面還是負面。 最初,我使用for循環在cmd中打印出tweet。 程序中循環的初始代碼

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新手,無法修復該錯誤。

您是否嘗試過使用

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM