从生成器对象中提取特定信息

时间:2015-12-07 23:59:51

标签: python json

这个问题已被提出(Extract data (likes) from JSON API using Python),但我无法得到他们的答案。特别是,我想打印输出中的某些字段。

正如原始问题中详述的那样,数据采用以下格式(类似于JSON),但实际上是一个"生成器对象。"

 {
    u'category': u'Product/Service',
    u'username': u'walkers',
    u'about': u"Welcome to the home of Walkers Crisps. When it comes to making Brits smile, we\u2019ve got it in the bag (yeah, we went there.) We're here Mon-Fri, 9am-6pm!",
    u'talking_about_count': 3076,
    u'description': u'To find out more about Walkers, visit:\nhttp://twitter.com/walkers_crisps\nhttp://www.youtube.com/walkerscrisps',
    u'has_added_app': False,
    u'can_post': True,
    u'cover': {
      u'source': u'https://scontent.xx.fbcdn.net/hphotos-xpt1/t31.0-8/s720x720/11165156_10153204315777649_4115137634691483959_o.jpg',
      u'cover_id': u'10153204315777649',
      u'offset_x': 0,
      u'offset_y': 0,
      u'id': u'10153204315777649'
    },
    u'name': u'Walkers',
    u'website': u'http://www.walkers.co.uk',
    u'link': u'https://www.facebook.com/walkers',
    u'likes': 552762,
    u'parking': {
      u'street': 0,
      u'lot': 0,
      u'valet': 0
    },
    u'is_community_page': False,
    u'were_here_count': 0,
    u'checkins': 0,
    u'id': u'53198517648',
    u'is_published': True
  }

因此,要提取特定字段(例如,"喜欢"),我们需要使用原始答案中建议的循环:

new_dictionary = {}
for name, value in datas:
    new_dictionary[name] = value

然而,当我尝试使用他们建议的代码时,我得到了错误" ValueError:解压缩的值太多了。"我理解错误的含义,但我不确定在这种情况下如何解决它。

0 个答案:

没有答案