我有一个名为obj_1
的字典对象,它的值显示如下:
{u'd':{u'results': [{u'Desc':u'This is a description...',
u'Title':u'This is a title...',
u'data': {u'Url': u'www.site.com'}},
{u'Desc':u'This is a description...',
u'Title':u'This is a title...',
u'data': {u'Url': u'www.site.com'}}]
}}
这是解码的json(使用requests
模块解码器解码)。如何仅提取u'Title'
值和URL值?我没有在任何教程中看到过这样的字典类型。
答案 0 :(得分:3)
假设你的“obj_1”真的是这样的:
obj_1 = {u'd':{u'results': [{u'Desc':u'This is a description...',
u'Title':u'This is a title...'},
{u'Desc':u'This is a description...',
u'Title':u'This is a title...'}]
}}
然后就这么简单:
titles = [d['Title'] for d in obj_1['d']['results']]
答案 1 :(得分:1)
>>> obj_1 = {u'd':{u'results': [{u'Desc':u'This is a description...',
... u'Title':u'This is a title...',
... u'data': {u'Url': u'www.site.com'}},
...
... {u'Desc':u'This is a description...',
... u'Title':u'This is a title...',
... u'data': {u'Url': u'www.site.com'}}]
... }}
>>> [(x[u'Title'], x[u'data'][u'Url']) for x in obj_1[u'd'][u'results']]
[(u'This is a title...', u'www.site.com'), (u'This is a title...', u'www.site.com')]