简体   繁体   中英

Python return “None” prevents following for loop

I'm using Tweepy to stream tweets. I'm trying to filter out retweeted content using the retweeted_status identifier in the json file. 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. The script seems to stop after the if statement:

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. Though for that to make sense, your if statement should be inside the for loop.

Replace return with continue and it should work great. continue skips the rest of the for loop and starts on the next value.

If you want an empty line to be printed out before the for loop, then replace return with print() and that will happen instead.

You should probably call another function if the new data from the on_status method contains things you want. 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.

Under the hood the Tweepy library is calling a inherited( StreamListener ) method from your custom StreamListener called on_data . 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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