python报错indentationerror_Python IndentationError unindent与任何外部缩进级别都不匹配

1586010002-jmsa.png

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

解决方案

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:

GgS30.png

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值