0

我有以下变量,我试图从中提取修订值..有人对如何做到这一点有任何意见吗?

u'revision': u'ad0beef66e5890cde6f0961ed03d8bc7e3defc63' 

输入:-

data ={u'status': u'MERGED', u'topic': u'', u'currentPatchSet': {u'approvals': [{u'by': {u'username': u'lnxbuild', u'name': u'Linux Build Service Account', u'email': u'lnxbuild@localhost'}, u'type': u'CRVW', u'description': u'Code Review', u'value': u'2', u'grantedOn': 1360897100}, {u'type': u'SUBM', u'by': {u'username': u'lnxbuild', u'name': u'Linux Build Service Account', u'email': u'lnxbuild@localhost'}, u'value': u'1', u'grantedOn': 1360897100}, {u'by': {u'username': u'lnxbuild', u'name': u'Linux Build Service Account', u'email': u'lnxbuild@localhost'}, u'type': u'TEST', u'description': u'Developer Verified', u'value': u'1', u'grantedOn': 1360897100}, {u'by': {u'username': u'lnxbuild', u'name': u'Linux Build Service Account', u'email': u'lnxbuild@localhost'}, u'type': u'VRIF', u'description': u'Verified', u'value': u'1', u'grantedOn': 1360897100}], u'createdOn': 1360897100, u'number': u'1', u'parents': [u'354a90cd1ae4ba9ce2a2a4ad38f8efbc8c02b02d', u'e4ffc07a6f78210a49f6ad2e0d705d7054c20ed6'], u'uploader': {u'username': u'lnxbuild', u'name': u'Linux Build Service Account', u'email': u'lnxbuild@localhost'}, u'ref': u'refs/changes/52/249452/1', u'revision': u'ad0beef66e5890cde6f0961ed03d8bc7e3defc63'}, u'url': u'https://review-android.company.com/249452', u'createdOn': 1360897100, u'number': u'249452', u'lastUpdated': 1360897121, u'project': u'platform/vendor/qcom-proprietary/wlan', u'sortKey': u'00231d720003ce6c', u'branch': u'master', u'owner': {u'username': u'lnxbuild', u'name': u'Linux Build Service Account', u'email': u'lnxbuild@localhost'}, u'open': False, u'id': u'Iad0beef66e5890cde6f0961ed03d8bc7e3defc63', u'subject': u'Merge "wlan: Enable ActiveModeOffload by default"'}

输出:-

 ad0beef66e5890cde6f0961ed03d8bc7e3defc63 
4

1 回答 1

2

只需按照“currentPatchSet”键然后按“revision”键即可检索它

data['currentPatchSet']['revision']

显示其工作原理的交互式示例:

>>> import pprint # pretty print for seeing the layout of the dict
>>> data ={u'status': u'MERGED', u'topic': u'', u'currentPatchSet': {u'approvals': [{u'by': {u'username': u'lnxbuild', u'name': u'Linux Build Service Account', u'email': u'lnxbuild@localhost'}, u'type': u'CRVW', u'description': u'Code Review', u'value': u'2', u'grantedOn': 1360897100}, {u'type': u'SUBM', u'by': {u'username': u'lnxbuild', u'name': u'Linux Build Service Account', u'email': u'lnxbuild@localhost'}, u'value': u'1', u'grantedOn': 1360897100}, {u'by': {u'username': u'lnxbuild', u'name': u'Linux Build Service Account', u'email': u'lnxbuild@localhost'}, u'type': u'TEST', u'description': u'Developer Verified', u'value': u'1', u'grantedOn': 1360897100}, {u'by': {u'username': u'lnxbuild', u'name': u'Linux Build Service Account', u'email': u'lnxbuild@localhost'}, u'type': u'VRIF', u'description': u'Verified', u'value': u'1', u'grantedOn': 1360897100}], u'createdOn': 1360897100, u'number': u'1', u'parents': [u'354a90cd1ae4ba9ce2a2a4ad38f8efbc8c02b02d', u'e4ffc07a6f78210a49f6ad2e0d705d7054c20ed6'], u'uploader': {u'username': u'lnxbuild', u'name': u'Linux Build Service Account', u'email': u'lnxbuild@localhost'}, u'ref': u'refs/changes/52/249452/1', u'revision': u'ad0beef66e5890cde6f0961ed03d8bc7e3defc63'}, u'url': u'https://review-android.company.com/249452', u'createdOn': 1360897100, u'number': u'249452', u'lastUpdated': 1360897121, u'project': u'platform/vendor/qcom-proprietary/wlan', u'sortKey': u'00231d720003ce6c', u'branch': u'master', u'owner': {u'username': u'lnxbuild', u'name': u'Linux Build Service Account', u'email': u'lnxbuild@localhost'}, u'open': False, u'id': u'Iad0beef66e5890cde6f0961ed03d8bc7e3defc63', u'subject': u'Merge "wlan: Enable ActiveModeOffload by default"'}

让我们可视化数据

>>> pprint.pprint(data)
{u'branch': u'master',
 u'createdOn': 1360897100,
 u'currentPatchSet': {u'approvals': [{u'by': {u'email': u'lnxbuild@localhost',
                                              u'name': u'Linux Build Service Account',
                                              u'username': u'lnxbuild'},
                                      u'description': u'Code Review',
                                      u'grantedOn': 1360897100,
                                      u'type': u'CRVW',
                                      u'value': u'2'},
                                     {u'by': {u'email': u'lnxbuild@localhost',
                                              u'name': u'Linux Build Service Account',
                                              u'username': u'lnxbuild'},
                                      u'grantedOn': 1360897100,
                                      u'type': u'SUBM',
                                      u'value': u'1'},
                                     {u'by': {u'email': u'lnxbuild@localhost',
                                              u'name': u'Linux Build Service Account',
                                              u'username': u'lnxbuild'},
                                      u'description': u'Developer Verified',
                                      u'grantedOn': 1360897100,
                                      u'type': u'TEST',
                                      u'value': u'1'},
                                     {u'by': {u'email': u'lnxbuild@localhost',
                                              u'name': u'Linux Build Service Account',
                                              u'username': u'lnxbuild'},
                                      u'description': u'Verified',
                                      u'grantedOn': 1360897100,
                                      u'type': u'VRIF',
                                      u'value': u'1'}],
                      u'createdOn': 1360897100,
                      u'number': u'1',
                      u'parents': [u'354a90cd1ae4ba9ce2a2a4ad38f8efbc8c02b02d',
                                   u'e4ffc07a6f78210a49f6ad2e0d705d7054c20ed6'],
                      u'ref': u'refs/changes/52/249452/1',
                      u'revision': u'ad0beef66e5890cde6f0961ed03d8bc7e3defc63',
                      u'uploader': {u'email': u'lnxbuild@localhost',
                                    u'name': u'Linux Build Service Account',
                                    u'username': u'lnxbuild'}},
 u'id': u'Iad0beef66e5890cde6f0961ed03d8bc7e3defc63',
 u'lastUpdated': 1360897121,
 u'number': u'249452',
 u'open': False,
 u'owner': {u'email': u'lnxbuild@localhost',
            u'name': u'Linux Build Service Account',
            u'username': u'lnxbuild'},
 u'project': u'platform/vendor/qcom-proprietary/wlan',
 u'sortKey': u'00231d720003ce6c',
 u'status': u'MERGED',
 u'subject': u'Merge "wlan: Enable ActiveModeOffload by default"',
 u'topic': u'',
 u'url': u'https://review-android.company.com/249452'}

所以修订似乎在 currentPatchSet 字典中:

>>> pprint.pprint(data['currentPatchSet'])
{u'approvals': [{u'by': {u'email': u'lnxbuild@localhost',
                         u'name': u'Linux Build Service Account',
                         u'username': u'lnxbuild'},
                 u'description': u'Code Review',
                 u'grantedOn': 1360897100,
                 u'type': u'CRVW',
                 u'value': u'2'},
                {u'by': {u'email': u'lnxbuild@localhost',
                         u'name': u'Linux Build Service Account',
                         u'username': u'lnxbuild'},
                 u'grantedOn': 1360897100,
                 u'type': u'SUBM',
                 u'value': u'1'},
                {u'by': {u'email': u'lnxbuild@localhost',
                         u'name': u'Linux Build Service Account',
                         u'username': u'lnxbuild'},
                 u'description': u'Developer Verified',
                 u'grantedOn': 1360897100,
                 u'type': u'TEST',
                 u'value': u'1'},
                {u'by': {u'email': u'lnxbuild@localhost',
                         u'name': u'Linux Build Service Account',
                         u'username': u'lnxbuild'},
                 u'description': u'Verified',
                 u'grantedOn': 1360897100,
                 u'type': u'VRIF',
                 u'value': u'1'}],
 u'createdOn': 1360897100,
 u'number': u'1',
 u'parents': [u'354a90cd1ae4ba9ce2a2a4ad38f8efbc8c02b02d',
              u'e4ffc07a6f78210a49f6ad2e0d705d7054c20ed6'],
 u'ref': u'refs/changes/52/249452/1',
 u'revision': u'ad0beef66e5890cde6f0961ed03d8bc7e3defc63',
 u'uploader': {u'email': u'lnxbuild@localhost',
               u'name': u'Linux Build Service Account',
               u'username': u'lnxbuild'}}

确实如此,在“修订”键下。我们检索它:

>>> print data['currentPatchSet']['revision']
ad0beef66e5890cde6f0961ed03d8bc7e3defc63
>>> 
于 2013-02-22T23:07:10.687 回答