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'}}]
}}

这是解码的 json(使用requests模块解码器解码)。如何u'Title'仅提取值和 URL 值?我在任何教程中都没有看到任何这样的字典类型。

4

2 回答 2

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']]
于 2012-07-06T10:41:50.063 回答
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')]
于 2012-07-06T11:03:13.633 回答