16

I'm a beginner in python,

I have this error :

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

I code with notepad++ in windows 8.1. I don't understand why I have this error, I have paid attention about 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()
Aruna
  • 11,959
  • 3
  • 28
  • 42
papiyoyo
  • 163
  • 1
  • 1
  • 4
  • check that you don't have mixed tabs and space for the indentation. (notepad++ editor's options have an option to always use a fixed number of spaces instead of tabs, I encourage you to use it) – Pac0 Sep 24 '20 at 11:19

7 Answers7

44

You are mixing tabs and spaces. Don't do that. Specifically, the __init__ function body is indented with tabs while your on_data method is not.

Here is a screenshot of your code in my text editor; I set the tab stop to 8 spaces (which is what Python uses) and selected the text, which causes the editor to display tabs with continuous horizontal lines:

highlighted code with tabs shown as lines

You have your editor set to expanding tabs to every fourth column instead, so the methods appear to line up.

Run your code with:

python -tt scriptname.py

and fix all errors that finds. Then configure your editor to use spaces only for indentation; a good editor will insert 4 spaces every time you use the TAB key.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Martijn, how do you know which whitespace is used in OP's code? (All I see in the post are spaces.) – ron rothman Nov 03 '14 at 18:47
  • @ron.rothman: you open the editor and check if there are tab jumps in the source. – Martijn Pieters Nov 03 '14 at 18:48
  • @Marjin, thanks for answering me. I have checked, tabs = 4 space. I have runned my code with '-tt' but I have the same position of the error. I use tabs a the beginning of the line and space between symbols etc. Is it wrong? – papiyoyo Nov 03 '14 at 18:51
  • @papiyoyo: Python assumes **8 spaces** for a tab. You are not *consistently* using tabs for indentation, you are mixing tabs and spaces. It is very hard to consistently use only tabs, which is why the Python styleguide *strongly* recommends you stick to spaces only. – Martijn Pieters Nov 03 '14 at 18:53
13

You have mixed indentation formatting (spaces and tabs)

On Notepad++

Change Tab Settings to 4 spaces

Go to Settings -> Preferences -> Tab Settings -> Replace by spaces

Fix the current file mixed indentations

Select everything CTRL+A

Click TAB once, to add an indentation everywhere

Run SHIFT + TAB to remove the extra indentation, it will replace all TAB characters to 4 spaces.

Seraf
  • 850
  • 1
  • 17
  • 34
  • 1
    I can't suggest and edit because the queue is full, but this setting is in Settings->Preferences->Language->Tab Settings. If you select Python, this should be the default behavior. The trick to add and remove an indent is helpful though. – Aaron Chamberlain Apr 20 '20 at 22:22
4

I had the same problem quite a few times. It happened especially when i tried to paste a few lines of code from an editor online, the spaces are not registered properly as 'tabs' or 'spaces'.

However the fix was quite simple. I just had to remove the spacing across all the lines of code in that specific set and space it again with the tabs correctly. This fixed my problem.

Aswin
  • 493
  • 7
  • 14
1

I recently ran into the same problem but the issue wasn't related to tabs and spaces but an odd Unicode character that appeared invisible to the eye in my IDE. Eventually, I isolated the problem and typed out the line exactly as it was and checked the git diff:

enter image description here

If you are able to isolate the line before the reported line and type it out again, you might find that it solves your problem. Won't tell you why it happened in the first place, but it at least gets rid of the issue.

Thomas
  • 701
  • 2
  • 8
  • 23
1

make sure """ comments are only a tab away and not 5 spaces

  • ..and spaces is the [preferred](https://www.python.org/dev/peps/pep-0008/#tabs-or-spaces) indentation, not tabs. – Gino Mempin Feb 07 '20 at 02:00
0

Python IndentationError unindent does not match any outer indentation level

# usr/bin/bash -tt

or

# usr/bin/python -tt
Community
  • 1
  • 1
Emack333
  • 660
  • 6
  • 11
0

i have done proper indentation with spaces but syoll it was throwing error. Then I removed al the spaces and use Tab then it started working. thx