本文介绍了Python IndentationError unindent与任何外部缩进级别都不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的初学者,

我遇到此错误:

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

我在windows 8.1中用notepad++进行编码.我不明白为什么会出现此错误,我已经关注制表符和空格.

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.

我想将数据保存在self.file中

I want to save data in self.file

这是我的代码:

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()

推荐答案

您正在混合制表符和空格.不要那样做具体来说,__init__函数主体使用制表符缩进,而您的on_data方法则没有制表符.

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.

这是我的文本编辑器中您的代码的屏幕截图;我将制表位设置为8个空格(Python使用的空格)并选择了文本,这使编辑器显示带有连续水平线的制表符:

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

并修复所有发现的错误.然后将编辑器配置为仅使用空格进行缩进;好的编辑器会在您每次使用键时插入4个空格.

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 key.

这篇关于Python IndentationError unindent与任何外部缩进级别都不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 09:18