Python IndentationError unindent 不匹配任何外部缩进级别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26720841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python IndentationError unindent does not match any outer indentation level
提问by papiyoyo
I'm a beginner in python,
我是初学者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.
我用notepad++in编码windows 8.1。我不明白为什么我会出现这个错误,我已经关注了制表符和空格。
I want to save data in self.file
我想在 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()
采纳答案by Martijn Pieters
You are mixing tabs and spaces. Don't do that. Specifically, the __init__function body is indented with tabs while your on_datamethod is not.
您正在混合制表符和空格。不要那样做。具体来说,__init__函数体用制表符缩进,而您的on_data方法不是。
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:
这是我的文本编辑器中代码的屏幕截图;我将制表位设置为 8 个空格(这是 Python 使用的)并选择文本,这会导致编辑器显示带有连续水平线的制表符:
You have your editor set to expanding tabs to every fourth column instead, so the methods appearto line up.
您已将编辑器设置为将选项卡扩展到每四列,因此这些方法似乎是对齐的。
Run your code with:
运行您的代码:
python -tt scriptname.py
and fix all errors that finds. Then configure your editor to use spaces onlyfor indentation; a good editor will insert 4 spaces every time you use the TABkey.
并修复发现的所有错误。然后将您的编辑器配置为仅使用空格作为缩进;一个好的编辑器会在你每次使用TAB密钥时插入 4 个空格。
回答by Emack333
Python IndentationError unindent does not match any outer indentation level
Python IndentationError unindent 不匹配任何外部缩进级别
# usr/bin/bash -tt
or
或者
# usr/bin/python -tt
回答by Seraf
You have mixed indentation formatting (spaces and tabs)
您有混合缩进格式(空格和制表符)
On Notepad++
在记事本++上
Change Tab Settings to 4 spaces
将选项卡设置更改为 4 个空格
Go to Settings-> Preferences-> Tab Settings-> Replace by spaces
转到设置->首选项->选项卡设置->用空格替换
Fix the current file mixed indentations
修复当前文件混合缩进
Select everything CTRL+A
全选CTRL+A
Click TABonce, to add an indentation everywhere
单击TAB一次,在各处添加缩进
Run SHIFT+ TABto remove the extra indentation, it will replace all TAB characters to 4 spaces.
运行SHIFT+TAB删除额外的缩进,它将所有 TAB 字符替换为 4 个空格。
回答by Aswin
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.
然而,修复很简单。我只需要删除该特定集中所有代码行的间距,然后正确地使用制表符将其间隔开。这解决了我的问题。
回答by Thomas
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 gitdiff:
我最近遇到了同样的问题,但问题与制表符和空格无关,而是与我的 IDE 中肉眼看不到的奇怪 Unicode 字符有关。最终,我隔离了问题并完全按原样输入了该行并检查了git差异:
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.
如果您能够在报告的行之前隔离该行并再次键入它,您可能会发现它解决了您的问题。不会告诉你为什么它首先发生,但它至少摆脱了这个问题。
回答by Ataur Rehman
make sure """ comments are only a tab away and not 5 spaces
确保 """ 评论只有一个制表符而不是 5 个空格


