【问题标题】:extract tweets from a text file (python)从文本文件中提取推文(python)
【发布时间】: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


【解决方案1】:

来自推文的 json 数据有时会丢失字段。试试这样的,

ids = []
for tweet in tweets:
    if 'id_str' in tweet:
        ids.append(tweet['id_str'])

或等价的,

ids = [tweet['id_str'] for tweet in tweets if 'id_str' in tweet]

【讨论】:

    【解决方案2】:
    import json
    
    tweets = []
    tweets.append(
            json.loads('{"a": 1}')
    )
    tweet = tweets[0]
    print(tweet)
    print( tweet['id_str'] )
    
    --output:--
    {'a': 1}
    
    Traceback (most recent call last):
      File "1.py", line 9, in <module>
        print( tweet['id_str'] )
    KeyError: 'id_str'
    

    还有:

    my_dict = {u"id_str": 1}
    print my_dict["id_str"]
    
    --output:--
    1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-05
      • 2010-09-24
      相关资源
      最近更新 更多