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
getattr
and itsdefault
parameter are what you’re after:If the attribute described by the second argument doesn’t exist, it will return the third argument.