Skip to main content
Post Closed as "Duplicate" by Karl Knechtel python
edited tags
Link
Elazar
  • 21.9k
  • 4
  • 51
  • 68
remove sensitive data
Source Link
Elazar
  • 21.9k
  • 4
  • 51
  • 68
Source Link
E Ajanthan
  • 75
  • 1
  • 1
  • 9

IndentationError with Python

I am currently trying to stream tweets for a project using Python, Elasticsearch and Kibana.

While running my Python script, I have an IndentationError and I don't understand why, can anyone help me through this problem ?

Thanks in advance.

My Python script :

import json
import tweepy
import textblob
import elasticsearch

from tweepy import OAuthHandler, Stream
from tweepy.streaming import StreamListener
from textblob import TextBlob
from elasticsearch import Elasticsearch

consumer_key = '...'
consumer_secret = '...'
access_token = '...'
access_token_secret = '...'

elastic_search = Elasticsearch()

class MyStreamListener(StreamListener):
    def on_data(self, data):
        dict_data = json.loads(data)
        tweet = TextBlob(dict_data["text"])
        
        print(tweet.sentiment.polarity)
        
        if tweet.sentiment.polarity < 0:
            sentiment = "negative"
        elif tweet.sentiment.polarity == 0:
            sentiment = "neutral"
        else:
            sentiment = "positive"
        
        print(sentiment)
        
        elastic_search.index(index="sentiment",
                 doc_type="test-type",
                 body={"author": dict_data["user"]["screen_name"],
                       "date": dict_data["created_at"],
                       "message": dict_data["text"],
                       "polarity": tweet.sentiment.polarity,
                       "subjectivity": tweet.sentiment.subjectivity,
                       "sentiment": sentiment})
        
        return True
    
    def on_failure(self, status):
        print(status)

if __name__ == '__main__':
    listener = MyStreamListener()
    
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    
    stream = Stream(auth, listener)
    stream.filter(track=['congress'])

# user_choice = input("Please choose a Hashtag... : ")
# retrieve_tweets = api.search(user_choice)

The error message :

File "sentiment.py", line 21
    tweet = TextBlob(dict_data["text"])
                                      ^
IndentationError: unindent does not match any outer indentation level