[英]How to get all the iterations in a list with none values included from a tweet?
I have set of tweets with 10 dictionaries in the list "tweets". 我在“ tweets”列表中设置了带有10个词典的tweets。 Each dictionary has several tweets.
每个字典都有几条推文。 The first tweet has 100 and the rest 9 have 15 each.
第一条推文有100条,其余9条各有15条。 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']
我从您的代码中看到您正在检查['place'] ['name']
Can you test your logic with the following filter logic without ['name']: 您能否使用不带['name']的以下过滤器逻辑来测试您的逻辑:
...
if (isinstance(tweets[j].get('statuses',[])[k].get('place', {}))) == dict:
...
The twitter api returns json, which is a dictionary
type in Python. twitter api返回json,这是Python中的
dictionary
类型。 When you are calling keys using dict[key]
syntax, this is called subscripting
. 使用
dict[key]
语法调用键时,这称为subscripting
。 Now, nested calls on a dict object are dependent on that object being a dictionary type: 现在,对dict对象的嵌套调用依赖于该对象为字典类型:
dict[a][b]
relies on dict[a]
being a dictionary with key b
being available. dict[a][b]
依赖于dict[a]
是具有键b
的字典。 If dict[a]
is a different type, say None
or int
, it is not subscriptable. 如果
dict[a]
是不同的类型,例如None
或int
,则它不能下标。 This means that there is not necessarily a get
attribute for that type. 这意味着不一定需要该类型的
get
属性。 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 这可以确保
check
是dict
类型的,因此可以用密钥下标
EDIT: Note that using dict[key]
syntax is not safe against KeyErrors
. 编辑:请注意,使用
dict[key]
语法对KeyErrors
并不安全。 If you want to avoid those, use get
: 如果要避免这些,请使用
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
它采用
dict.get(key, <return_value>)
,其中return_value
默认为None
To make your program a bit more readable and avoid the inevitable infinite loop, ditch the while
loop: 为了使您的程序更具可读性并避免不可避免的无限循环,请抛弃
while
循环:
# 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. 这样就解决了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.