简体   繁体   English

Python返回“无”可防止跟随for循环

[英]Python return “None” prevents following for loop

I'm using Tweepy to stream tweets. 我正在使用Tweepy流推文。 I'm trying to filter out retweeted content using the retweeted_status identifier in the json file. 我正在尝试使用json文件中的retweeted_status标识符过滤掉转发的内容。 I want the for loop to execute after the empty return on the line before it, but it doesn't seem to work- nothing gets printed out. 我希望for循环在它之前的行上的空返回之后执行,但是它似乎不起作用-没有输出任何内容。 The script seems to stop after the if statement: 该脚本似乎在if语句之后停止:

class StreamListener(tweepy.StreamListener):


        def on_status(self, status):
                #these variable split out the data from the outputted json
                text = status.text
                name = status.user.screen_name
                id_str = status.id_str

#This if argument ensures that if there is retweeted status then None is returned
                if hasattr (status, 'retweeted_status'):
                        return
                for url in status.entities['urls']:

                        print (text, name, (url['expanded_url']), file=open("results.txt", "a"))

Instead of using return (Which will exit your function completely), you just want to go on to the next iteration of the for loop without printing. 而不是使用return(将完全退出函数),您只想继续进行for循环的下一个迭代而无需打印。 Though for that to make sense, your if statement should be inside the for loop. 尽管这样做很有意义,但是您的if语句应该在for循环内。

Replace return with continue and it should work great. 替换returncontinue ,它应该工作的伟大。 continue skips the rest of the for loop and starts on the next value. continue略过剩下的for循环,并开始对下一个值。

If you want an empty line to be printed out before the for loop, then replace return with print() and that will happen instead. 如果您希望在for循环之前打印出空行,请用print()替换return ,而这将会发生。

You should probably call another function if the new data from the on_status method contains things you want. 如果on_status方法中的新数据包含所需的内容,则可能应该调用另一个函数。 There is no need to do a continue inside of the on_status method because it's not a for loop, unless you create your own for loop and decide to continue based on your own custom business logic. 无需在on_status方法内进行继续操作,因为它不是for循环,除非您创建自己的for循环并根据自己的自定义业务逻辑决定继续。

Under the hood the Tweepy library is calling a inherited( StreamListener ) method from your custom StreamListener called on_data . 在幕后,Tweepy库正在从名为on_data的自定义StreamListener调用InheritedStreamListener )方法。 The only thing you're responsible for is doing something with that data or not. 您唯一要做的就是对数据进行处理或不进行处理。

def handle_status_update(status):
    # Handle your custom stuff in here...
    text = status.text
    name = status.user.screen_name
    id_str = status.id_str

    for url in status.entities['urls']:
        print (text, name, (url['expanded_url']), file=open("results.txt", "a"))


class StreamListener(tweepy.StreamListener):


        def on_status(self, status):
            # if not retweeted
            if not hasattr (status, 'retweeted_status'):
                handle_status_update(status)

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

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