2017-08-29 36 views
0

我目前正在處理一個代碼,以流twitter的帖子,並將它們保存到json文件。同時,textblob決定推文的情緒。 目前爲止所有工作都正常,但並未將所有輸出保存到文件中。它目前保存推文,但不保存textblob計算的情感分數。這是在Python我的第一天編碼和我欣賞的幫助每一位:)Python嘰嘰喳喳流保存到文件

import textblob as textblob 
from tweepy import Stream 
from tweepy import OAuthHandler 
from tweepy.streaming import StreamListener 
import json 
from textblob import TextBlob 

# consumer key, consumer secret, access token, access secret. 
consumer_key = x 
consumer_secret = x 
access_token = x 
access_token_secret = x 


class StdOutlistener(StreamListener): 
    def on_data(self, data): 
     all_data = json.loads(data) 
     tweet = TextBlob(all_data["text"]) 
     print(tweet) 
     print(tweet.sentiment) 

     # Open json text file to save the tweets 
     With open('tweets.json', 'a') as tf: 
      tf.write(data) 

     return True 

    def on_error(self, status): 
     print(status) 


auth = OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token, access_token_secret) 

twitterStream = Stream(auth, StdOutlistener()) 
twitterStream.filter(languages=["en"], track=["Test"]) 
+0

到底什麼是你的問題? –

+0

O我看到我有點不清楚:1:我想結合tweets和情緒。 2:我想知道如何將推文和情緒寫入json文件。傑克做了一些假設,他們是正確的:) – Robbert

回答

1

首先你確定要使用on_data而非on_statusthis詳細說明了兩者的區別。我不太熟悉tweepy,因此可能在這個計數上是錯誤的。

其次,您似乎沒有正確更新關於情緒的數據。你用tweet = TextBlob(all_data['text'])來計算它,但是不要對tweet變量或all_data變量做進一步處理。你想要什麼就像all_data['sentiment'] = tweet.sentiment

最後,您在最後沒有正確地將數據寫入文件。我假設您希望該文件是JSON條目的集合而不是單個JSON文檔。你正在做的是將提供的字符串data寫到文件的末尾,沒有換行符,而不是任何更新的字典。您將改爲將all_data字典作爲JSON對象寫入文件。

我的上述觀點的一個例子的解決將是:

import textblob as textblob 
from tweepy import Stream 
from tweepy import OAuthHandler 
from tweepy.streaming import StreamListener 
import json 
from textblob import TextBlob 

# consumer key, consumer secret, access token, access secret. 
consumer_key = x 
consumer_secret = x 
access_token = x 
access_token_secret = x 


class StdOutlistener(StreamListener): 
    def on_data(self, data): 
     all_data = json.loads(data) 
     tweet = TextBlob(all_data["text"]) 

     #Add the 'sentiment data to all_data 
     all_data['sentiment'] = tweet.sentiment 

     print(tweet) 
     print(tweet.sentiment) 

     # Open json text file to save the tweets 
     With open('tweets.json', 'a') as tf: 
      # Write a new line 
      tf.write('\n') 

      # Write the json data directly to the file 
      json.dump(all_data, tf) 
      # Alternatively: tf.write(json.dumps(all_data)) 

     return True 

    def on_error(self, status): 
     print(status) 


auth = OAuthHandler(consumer_key, consumer_secret) 
auth.set_access_token(access_token, access_token_secret) 

twitterStream = Stream(auth, StdOutlistener()) 
twitterStream.filter(languages=["en"], track=["Test"]) 
+0

嗨,謝謝!這正是我所尋找的和超級解釋。關於json數據;我不知道寫一個新的行是必要的。但是你的假設是正確的;我不想要一個單一的json文檔 – Robbert

+0

只不過,當你追加時,python不會自動寫一個新行。如果新行被寫入,那麼你只需簡單地使用「{} {} ...」就行了。另一個解決方案是始終確保在數據的末尾寫入新的行字符,這將獲得基本相同的結果。 –