I have set of tweets with 10 dictionaries in the list "tweets". Each dictionary has several tweets. The first tweet has 100 and the rest 9 have 15 each. I need the location of each tweet in all the dictionaries. When I try to iterate the values from a list it shows this error.
if (type(tweets[j]['statuses'][k]['place']['name'])) != None:
TypeError: 'NoneType' object is not subscriptable
The code I have used for the iteration is
for j in range (0,10):
while j == 0:
for k in range(0,100):
st1 = tweets[j]['statuses'][k]['place']['name']
print(st1)
I tried using "filter" to take out the "None" values, even that is not working. not every tweet has a location tagged to it. so it has None values. I need to print the locations of the tweets that are tagged.
Have you tried to check if the 'place' key is first available. I can see from your code that you are checking for ['place']['name']
Can you test your logic with the following filter logic without ['name']:
...
if (isinstance(tweets[j].get('statuses',[])[k].get('place', {}))) == dict:
...
The twitter api returns json, which is a dictionary
type in Python. When you are calling keys using dict[key]
syntax, this is called subscripting
. Now, nested calls on a dict object are dependent on that object being a dictionary type:
dict[a][b]
relies on dict[a]
being a dictionary with key b
being available. If dict[a]
is a different type, say None
or int
, it is not subscriptable. This means that there is not necessarily a get
attribute for that type. A simple way to fix this would be the following:
check = tweets[j]['statuses'][k]['place']
if isinstance(check, dict):
# do things
This makes sure that check
is of type dict
and therefore can be subscripted with a key
EDIT: Note that using dict[key]
syntax is not safe against KeyErrors
. If you want to avoid those, use get
:
my_dictionary = {'a': 1, 'b': 2}
my_dictionary['c'] # Raises KeyError: 'c' not in dictionary
my_dictionary.get('c') # returns None, no KeyError
It takes the form dict.get(key, <return_value>)
, where return_value
defaults to None
To make your program a bit more readable and avoid the inevitable infinite loop, ditch the while
loop:
# This will get individual tweets
for tweet in tweets:
# Returns all statuses or empty list
statuses = tweet.get('statuses', [])
for status in statuses:
if not isinstance(status, dict):
continue # will jump to next iteration of inner for loop
# Will be a name or None, if empty dict is returned from place
name = status.get('place', {}).get('name')
if name:
print(name)
for element in tweets:
for s in element.get('statuses'):
place = s.get('place')
print(place['name'])
This fixed it.
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.