Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python IndentationError unindent does not match any outer indentation level [duplicate]

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()
like image 295
papiyoyo Avatar asked Nov 03 '14 18:11

papiyoyo


People also ask

What does IndentationError Unindent does not match any outer indentation level?

The “indentationerror: unindent does not match any outer indentation level” error is raised when you use both spaces and tabs to indent your code. To solve this error, check to make sure that all your code uses either spaces or tabs in a program.

How do I fix an indentation error in Python?

To enable the option in order to view the tabs or whitespaces, take a look at the image below. Another option to get out of this problem is to use the built in Indent Guide in the Python interpreter. Enabling this option will guide through each line of code and show you exactly where your error lies.

What is outer indentation in Python?

Indentation in Python is important, and it makes your code well structured and clean. Python uses indentation to define code blocks. You can use either tabs or spaces to indent the code. However, using a combination of space and tab will result in indentationerror: unindent does not match any outer indentation level.

How do I fix indentation error in Google Colab?

Here's how I fixed it. Before pasting the copied code into Colab, first put it into notepad++. Go to View> Show Symbols >Show All Characters, click on this, you should now be able so see all the characters in the code.


2 Answers

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.

like image 131
Martijn Pieters Avatar answered Oct 07 '22 05:10

Martijn Pieters


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.

like image 25
Seraf Avatar answered Oct 07 '22 04:10

Seraf