【发布时间】:2013-05-13 20:42:50
【问题描述】:
抱歉,我只是想将每条推文中的 'id_str' 存储到一个名为 ids[] 的新列表中。 但收到以下错误:
Traceback(最近一次调用最后一次): 文件“extract_tweet.py”,第 17 行,在 打印推文['id_str'] KeyError: 'id_str'
我的代码是:
import json
import sys
if __name__ == '__main__':
tweets = []
for line in open (sys.argv[1]):
try:
tweets.append(json.loads(line))
except:
pass
ids = []
for tweet in tweets:
ids.append(tweet['id_str'])
【问题讨论】:
-
打印出
tweets并确保它是您所期望的。它有'id_str'键吗? -
u'contributors', u'truncated', u'text', u'in_reply_to_status_id', u'id', u'favorite_count', u'source', u'retweeted', u'坐标',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' lang', u'created_at', u'filter_level', u'in_reply_to_status_id_str', u'place']
-
我知道推文中的 json 有时会丢失字段。在附加到
ids之前,我会尝试使用if 'id_str' in tweet:。 -
我想我会把它作为解决方案发布给任何未来的访问者。
标签: python json twitter tweets