简体   繁体   English

python:缩进错误:预期缩进的块

[英]python: Indentation Error: expected an indented block

I try to get data from twitter by using API and tweepy python. 我尝试通过使用API​​和tweepy python从twitter获取数据。 After install successfully python and tweepy, a try to get data.. Here is my code 成功安装python和tweepy之后,尝试获取数据。这是我的代码

enter code here
from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
ckey = 'YIlpVeuTTqjJhWbFj6kmF2ecL'
csecret ='rDUpUgmJF6ZVFYsKGbLW43JXoX35mTzQW7JoPWCQwUh2sBhEaM' 
atoken = '3262955994-MwFta8wXh0mjIXatX12BL5Smw2ABZtomMLD782E'
asecret='hcyYCOuBBEsrq3MtBpwI2IQqAqIj4wHpCH0rAODR9C6g6'
class listener(StreamListener):
def on_data(self, data):
        print data
        return True

def on_error(self, status):
    print status

auth = OAuthHandler(ckey,csecret)
auth.set_access_token(atoken,asecret)
twitterStream= Stream(auth,listener())
twitterStream.filter(track=['python', 'javascript', 'ruby'])

When I run it, i had an error: 运行它时,出现错误:

file 'ex.py' ,line 10
   print data
       ^
 IndentationError: expected an indented block

I got this code on the Internet.Thanks 我在互联网上获得了此代码。

Looks like you indented twice on that line. 好像您在该行上缩进了两次。 Instead indent on_data() the same way as it is done for on_error() (that is, 4 spaces for one level of indentation). 而是以与on_data()相同的方式缩进on_data() (即,对于一个缩进级别, on_error() 4个空格)。

Seems like you are using uneven indentation, I can see 8 spaces in these lines - 好像您使用的是不均匀的缩进,我在这些行中可以看到8个空格-

def on_data(self, data):
        print data
        return True

where as only 4 in line - 其中只有4行-

def on_error(self, status):
    print status

I am guessing you have more code where you are using 4 spaces for one level of indentation. 我猜想您有更多的代码,其中您将4个空格用于一个缩进级别。

You should use constant amount of spaces for indentation throughout your code. 您应该在整个代码中使用固定数量的空格来缩进。 I would advice you to try changing the indentation of the function to 4 spaces and check if that fixes your issue. 我建议您尝试将函数的缩进更改为4个空格,并检查是否可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM