Python Key 存在时出现 KeyError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24816114/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
KeyError when Key exists
提问by daneshjai
Using python and twitter api to get tweet objects.
使用python和twitter api获取tweet对象。
I have a file (tweetfile = a .txt file on my computer) with tweets and I'm trying to loop through the objects to get the text. I checked the twitter object with tweetObj.keys() to see the keys and 'text' is there; however, when I try to get the individual text using tweetObj['text'] I get the KeyError: 'text'
我有一个包含推文的文件(tweetfile = 我计算机上的 .txt 文件),我正在尝试遍历对象以获取文本。我用 tweetObj.keys() 检查了 twitter 对象以查看键和“文本”在那里;但是,当我尝试使用 tweetObj['text'] 获取单个文本时,我得到了 KeyError: 'text'
code:
代码:
for line in tweetfile:
tweetObj = json.loads(line)
keys = tweetObj.keys()
print keys
tweet = tweetObj['text']
print tweet
below is the output:
下面是输出:
[u'contributors', u'truncated', u'text', u'in_reply_to_status_id', u'id', u'favorite_count', u'source', u'retweeted', u'coordinates', u'entities', u'in_reply_to_screen_name', u'id_str', u'retweet_count', u'in_reply_to_user_id', u'favorited', u'user', u'geo', u'in_reply_to_user_id_str', u'possibly_sensitive', u'lang', u'created_at', u'filter_level', u'in_reply_to_status_id_str', u'place']
@awe5sauce my dad was like "so u wanna be in a relationship with a 'big dumb idiot'" nd i was like yah shes the bae u feel lmao
[u'delete']
Traceback (most recent call last):
File "C:\apps\droid\a1\tweets.py", line 34, in <module>
main()
File "C:\apps\droid\a1\tweets.py", line 28, in main
tweet = tweetObj['text']
KeyError: 'text'
I'm not sure how to approach since it looks like it prints one tweet. The question is why would this occur where the key exists and appears to return a value but not for all instances and how can I correct it to where I can access the value for all lines with that key?
我不确定如何处理,因为它看起来像是打印了一条推文。问题是为什么这会发生在键存在并且似乎返回一个值但不是所有实例的地方,我如何将其纠正为我可以使用该键访问所有行的值?
采纳答案by ssm
There are 2 dictionaries created within the loop, one for each line. The first one has text
and the second one only has a 'delete'
key. It does not have the 'text'
key. Hence the error message.
在循环中创建了 2 个字典,每行一个。第一个有text
,第二个只有'delete'
钥匙。它没有'text'
钥匙。因此出现错误消息。
Change it to:
将其更改为:
for line in tweetfile:
tweetObj = json.loads(line)
keys = tweetObj.keys()
print keys
if 'text' in tweetObj:
print tweetObj['text']
else:
print 'This does not have a text entry'
Just so you know, if you are only interested in the lines containing text
, you may want to use
只是让您知道,如果您只对包含 的行感兴趣,则text
可能需要使用
[ json.loads(l)['text'] for l in tweetfile if 'text' in json.loads(l) ]
[ json.loads(l)['text'] for l in tweetfile if 'text' in json.loads(l) ]
or
或者
'\n'.join([ json.loads(l)['text'] for l in tweetfile if 'text' in json.loads(l) ])
'\n'.join([ json.loads(l)['text'] for l in tweetfile if 'text' in json.loads(l) ])
or even BETTER
甚至更好
[ json.loads(l).get('text') for l in tweetfile]
[ json.loads(l).get('text') for l in tweetfile]