2017-11-01 2 views
1

현재 Python, Elasticsearch 및 Kibana를 사용하여 프로젝트의 트윗을 스트리밍하려고합니다.들여 쓰기 Python과 함께 들여 쓰기

내 Python 스크립트를 실행하는 동안 IndentationError가 발생하며 왜이 문제를 해결할 수 있습니까?

미리 감사드립니다.

내 파이썬 스크립트를

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) 

오류 메시지 :

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

당신이 탭을 사용하고 있습니까 : SO 사이트의 표현이 공간에 탭을 변환하지만, 에디터 모드가 탭을 보여 것을

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) 

주? '\ t'를 검색하고 4 칸으로 대체하십시오. – Elazar

+0

예를 들어 여기를 참조하십시오. (https://stackoverflow.com/questions/17076302/possible-mixed-indentation-in-python) – Elazar

+0

안녕하세요 @ 엘라 자르, 탭을 사용하지 않고 4 칸을 사용했습니다. –

답변

3

당신은 거기 탭을해야합니까. 혹시

indentation

+0

어떻게 알 수 있습니까? E Ajanthan의 텍스트를 복사하면 모든 공백이 생깁니다. – Teodor

+1

편집기 모드에서 복사하십시오. – Elazar

+0

@Elazar 멋진 팁 - 나는 그걸 알아 내지 못했습니다. – SiHa

관련 문제