skip to Main Content

I have tweepy object (containing twitter data) where not all the content in the object has every attribute. Whenever there is no attribute I want to append a None value. I have come up with the following code but it’s just too long, since there is a lot of attributes and I am applying a try-except block for each one. I have just shown the first 3 attributes since there is a whole lot em. Just curious if there is a better way of doing this?

Note: That an exception with Attribute error must be added since not all the content has all the attributes and therefore an error will be thrown whenever it’s iterating content that doesn’t have that attribute.
For example during the first iteration tweet.author,tweet.contributors,tweet.coordinates might be present. But during the second iteration only tweet.contributors,tweet.coordinates might be present, and that when the python throws an AttributeError

 from tweepy_streamer import GetTweets
 inst = GetTweets()
 # tweepy API object containing twitter data such as tweet, user etc
 twObj = inst.stream_30day_tweets(keyword = 'volcanic disaster', search_from = '202009010000', search_to = '202009210000')

 tweet_list = []

for tweet in twObj:
    try:
        author = tweet.author
    except AttributeError:
        author = None
    try:
        contributors = tweet.contributors
    except AttributeError:
        contributors =None 
    try:
        coordinates = tweet.coordinates
    except AttributeError:
        coordinates = None
 
    # Append to a list of dictionaries in order to construct a dataframe
    tweet_list.append({
        'author' : author,
        'contributors' : contributors,
        'coordinates' : coordinates,
        })

2

Answers


  1. getattr and its default parameter are what you’re after:

    author = getattr(tweet, "author", None)
    contributors = getattr(tweet, "contributors", None)
    coordinates = getattr(tweet, "coordinates", None)
    

    If the attribute described by the second argument doesn’t exist, it will return the third argument.

    Login or Signup to reply.
  2. properties = ('author', 'contributors', 'coordinates')
    for tweet in twObj:
        tweet_list.append({key:getattr(tweet, key, None) for key in properties})
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search