1

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
Elazar
  • 20,415
  • 4
  • 46
  • 67
E Ajanthan
  • 77
  • 1
  • 1
  • 9

1 Answers1

4

You do have tabs there.

    def on_data(self, data):
        dict_data = json.loads(data)
# ^ tab and 4 spaces here
        tweet = TextBlob(dict_data["text"])
# ^ 8 spaces here           
        print(tweet.sentiment.polarity)
# ^ ^ two tabs here (equal 16 spaces)

Note that the representation in SO site translates the tabs to spaces, but if you copy the source into a code editor, it reveals the tabs:

indentation

wjandrea
  • 28,235
  • 9
  • 60
  • 81
Elazar
  • 20,415
  • 4
  • 46
  • 67
  • How can you tell that? When I copy E Ajanthan's text, I get all spaces. – Teodor Nov 01 '17 at 10:02
  • @Elazar Great tip - I'd not figured that one out. – SiHa Nov 01 '17 at 10:05
  • @EAjanthan: That's because you have *lots* of tabs (on 25 different lines). – SiHa Nov 01 '17 at 10:08
  • How can I see all the tabs using Notepad ? – E Ajanthan Nov 01 '17 at 10:11
  • 1
    @EAjanthan Don't use notepad. It's not a code editor. Use Notepad++, VSCode, Atom, Vim or any other _code_ editor. Also, execute your code using `python -tt ` – Elazar Nov 01 '17 at 10:12
  • I wasn't sure what you meant by "the editor mode", so I tried to clarify it. LMK if it's incorrect. I'm trying to close [another question](https://stackoverflow.com/q/68278846/4518341) as a duplicate, and OP there is new, so I want to make it extra clear for them. – wjandrea Jul 07 '21 at 04:56