Python IndentationError unindent does not match outer indent level

I'm new to python,

I have this error:

Error : 
def on_data(self,data):
                      ^
IdentationError : unindent does not match any outer indentation level

I am code from notepad++v windows 8.1. I don’t understand why I have this error, I paid attention to the tabs and space.

I want to save data in self.file

Here is my code:

from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
from tweepy import Stream
import tweepy
import time



class StdOutListener(StreamListener):

    def __init__(self,file):
        self.file = file

    def on_data(self, data):

        print data
        self.file.write(data)
        return True

    def on_error(self, status):
        print status


def main() :
    file = open('work.txt','w')
    listn = StdOutListener(file)
    consumer_key=""
    consumer_secret=""

    access_token=""
    access_token_secret=""

    auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)
    #api = tweepy.API(auth)

    #filename=open('helloworld.txt','r')
    #f=filename.readlines()
    #filename.close()

    #for line in f:
    #   api.update_status(line)

    stream = Stream(auth, listn)
    stream.filter(track=['lol'])
    file.close()
+4
source share
4 answers

You mix tabs and spaces. Do not do that. In particular, the body of the function __init__has tabbed indentation, while your method is on_datanot.

; - 8 ( Python) , :

selected code with tabs shown as strings

, , .

python -tt scriptname.py

. ; 4 , TAB.

+19

++, ctrl + a, TAB , , shift + tab, . 4 . , → → → . 4 .

+4

. , , "" "".

However, the fix was pretty simple. I just needed to remove the spacing on all lines of code in that particular set and again the tabbed space correctly. This fixed my problem.

+2
source
0
source

All Articles