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()
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.
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.
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.
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.
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:
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.
You have mixed indentation formatting (spaces and tabs)
Go to Settings -> Preferences -> Tab Settings -> Replace by spaces
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With