用variab中的大量数据构造字典

2024-10-01 04:57:48 发布

您现在位置:Python中文网/ 问答频道 /正文

变量中有以下数据。我的目标是构造一个以数字为索引的字典,它的值是url、owner email和commitMessage。我需要一些关于如何做到这一点的想法,或者如果有一个更好的执行这一点。我真的很感谢你的意见。你知道吗

{
"project": "platform/vendor/company-proprietary/radio",
"branch": "master",
"id": "I2d46a51ea94d66eefe044e633b15c6eaa7dbb2af",
"number": "147441",
"subject": "radio: Update radio driver version",
"owner": {
    "name": "Shailender bary",
    "email": "bary@ca.company.com",
    "username": "bary"
},
"url": "https://company/147441",
"commitMessage": "radio: Update radio driver version\n\nUpdate radio driver version for the FC release\n\nChange-Id: I2d46a51ea94d66eefe044e633b15c6eaa7dbb2af\n",
"createdOn": 1344628179,
"lastUpdated": 1357170699,
"sortKey": "00222ad700023ff1",
"open": true,
"status": "NEW",
"currentPatchSet": {
    "number": "2",
    "revision": "18bb2f7af0dc7cad7b3959c23d886a1a48c8f728",
    "parents": [
        "96968973a0dfa1ce105cdec7c91d3304af0d8393"
    ],
    "ref": "refs/changes/41/147441/2",
    "uploader": {
        "name": "Shailender bary",
        "email": "bary@ca.company.com",
        "username": "bary"
    },
    "createdOn": 1344628312,
    "approvals": [
        {
            "type": "VRIF",
            "description": "Verified",
            "value": "-1",
            "grantedOn": 1344654401,
            "by": {
                "name": "Linux Build Service Account",
                "email": "lnxbuild@localhost",
                "username": "lnxbuild"
            }
        },
        {
            "type": "CRVW",
            "description": "Code Review",
            "value": "-2",
            "grantedOn": 1357170699,
            "by": {
                "name": "tom Johnson",
                "email": "jjohnson@ca.company.com",
                "username": "jjohnson"
            }
        },
        {
            "type": "CRVW",
            "description": "Code Review",
            "value": "1",
            "grantedOn": 1344628453,
            "by": {
                "name": "Shailender bary",
                "email": "bary@ca.company.com",
                "username": "bary"
            }
        },
        {
            "type": "TEST",
            "description": "Developer Verified",
            "value": "1",
            "grantedOn": 1344628453,
            "by": {
                "name": "Shailender bary",
                "email": "bary@ca.company.com",
                "username": "bary"
            }
        },
        {
            "type": "CRVW",
            "description": "Code Review",
            "value": "2",
            "grantedOn": 1344629368,
            "by": {
                "name": "johnb Bhatia",
                "email": "johnbb@ca.company.com",
                "username": "johnbb"
            }
        }
    ],
    "files": [
        {
            "file": "/COMMIT_MSG",
            "type": "ADDED"
        },
        {
            "file": "rime/CORE/MAC/inc/qradio_version.h",
            "type": "MODIFIED"
        }
    ]
}

{
"project": "platform/vendor/company-proprietary/radio",
"branch": "master",
"id": "I2c9c0515974a3f69b4341cf5e1e2c1b17ea10641",
"number": "143446",
"subject": "radio: Changes done for P2P Device interface.",
"owner": {
    "name": "Mohit Chang",
    "email": "mChang@ca.company.com",
    "username": "mChang"
},
"url": "https://company/143446",
"commitMessage": "radio: Changes done for P2P Device interface.\n\nChange-Id: I2c9c0515974a3f69b4341cf5e1e2c1b17ea10641\n",
"createdOn": 1343352861,
"lastUpdated": 1357170602,
"sortKey": "00222ad600023056",
"open": true,
"status": "NEW",
"currentPatchSet": {
    "number": "1",
    "revision": "743006ffcc16a725c7499aaffa1594681488e1f8",
    "parents": [
        "9bef0f406dfbd7bbd747d20a41b68fc1c2fd7c48"
    ],
    "ref": "refs/changes/46/143446/1",
    "uploader": {
        "name": "Mohit Chang",
        "email": "mChang@ca.company.com",
        "username": "mChang"
    },
    "createdOn": 1343352861,
    "approvals": [
        {
            "type": "VRIF",
            "description": "Verified",
            "value": "1",
            "grantedOn": 1343360579,
            "by": {
                "name": "Linux Build Service Account",
                "email": "lnxbuild@localhost",
                "username": "lnxbuild"
            }
        },
        {
            "type": "CRVW",
            "description": "Code Review",
            "value": "-2",
            "grantedOn": 1357170602,
            "by": {
                "name": "tom Johnson",
                "email": "jjohnson@ca.company.com",
                "username": "jjohnson"
            }
        },
        {
            "type": "CRVW",
            "description": "Code Review",
            "value": "1",
            "grantedOn": 1343353135,
            "by": {
                "name": "Shailender bary",
                "email": "bary@ca.company.com",
                "username": "bary"
            }
        },
        {
            "type": "TEST",
            "description": "Developer Verified",
            "value": "1",
            "grantedOn": 1343353135,
            "by": {
                "name": "Shailender bary",
                "email": "bary@ca.company.com",
                "username": "bary"
            }
        },
        {
            "type": "CRVW",
            "description": "Code Review",
            "value": "2",
            "grantedOn": 1343355565,
            "by": {
                "name": "johnb Bhatia",
                "email": "johnbb@ca.company.com",
                "username": "johnbb"
            }
        }
    ],
    "files": [
        {
            "file": "/COMMIT_MSG",
            "type": "ADDED"
        },
        {
            "file": "rime1_2/CORE/HDD/inc/radio_hdd_main.h",
            "type": "MODIFIED"
        },
        {
            "file": "rime1_2/CORE/HDD/src/radio_hdd_cfg80211.c",
            "type": "MODIFIED"
        },
        {
            "file": "rime1_2/CORE/HDD/src/radio_hdd_hostapd.c",
            "type": "MODIFIED"
        },
        {
            "file": "rime1_2/CORE/HDD/src/radio_hdd_main.c",
            "type": "MODIFIED"
        }{
            "file": "rime1_2/CORE/HDD/src/radio_hdd_p2p.c",
            "type": "MODIFIED"
        }
    ]
}

}


Tags: namecombyvalueemailtypeusernamedescription
3条回答

好吧,也许只是写一个自定义函数从中提取数据?也许像下面这样。你知道吗

instance = {"project":"platform/vendor/company-proprietary/radio","branch":"master","id":"I2d46a51ea94d66eefe044e633b15c6eaa7dbb2af","number":"147441","subject":"radio: Update radio driver version","owner":{"name":"Shailender bary","email":"bary@ca.company.com","username":"bary"},"url":"https://company/147441","commitMessage":"radio: Update radio driver version\n\nUpdate radio driver version for the FC release\n\nChange-Id: I2d46a51ea94d66eefe044e633b15c6eaa7dbb2af\n","createdOn":1344628179,"lastUpdated":1357170699,"sortKey":"00222ad700023ff1","open":true,"status":"NEW","currentPatchSet":{"number":"2","revision":"18bb2f7af0dc7cad7b3959c23d886a1a48c8f728","parents":["96968973a0dfa1ce105cdec7c91d3304af0d8393"],"ref":"refs/changes/41/147441/2","uploader":{"name":"Shailender bary","email":"bary@ca.company.com","username":"bary"},"createdOn":1344628312,"approvals":[{"type":"VRIF","description":"Verified","value":"-1","grantedOn":1344654401,"by":{"name":"Linux Build Service Account","email":"lnxbuild@localhost","username":"lnxbuild"}},{"type":"CRVW","description":"Code Review","value":"-2","grantedOn":1357170699,"by":{"name":"tom Johnson","email":"jjohnson@ca.company.com","username":"jjohnson"}},{"type":"CRVW","description":"Code Review","value":"1","grantedOn":1344628453,"by":{"name":"Shailender bary","email":"bary@ca.company.com","username":"bary"}},{"type":"TEST","description":"Developer Verified","value":"1","grantedOn":1344628453,"by":{"name":"Shailender bary","email":"bary@ca.company.com","username":"bary"}},{"type":"CRVW","description":"Code Review","value":"2","grantedOn":1344629368,"by":{"name":"johnb Bhatia","email":"johnbb@ca.company.com","username":"johnbb"}}],"files":[{"file":"/COMMIT_MSG","type":"ADDED"},{"file":"rime/CORE/MAC/inc/qradio_version.h","type":"MODIFIED"}]}}


#Say data is the dictionary you want to save the data to.
data = {}

def sanitize_data(variable):
    """ A simple function to sanitize data"""
    instance_data = {}

    level_one = ("url", "commitMessage")
    level_two = ("name", "email")

    for x in level_one:
        instance_data[x] = variable[x]

    for x in level_two:
        instance_data[x] = variable["owner"][x]

    index = len(data)
    data[index] = instance_data
    print data


>>>sanitize_data(instance)
{0: {'url': 'https://company/147441', 'commitMessage': 'radio: Update radio driver version\n\nUpdate radio driver', 'name': 'Shailenderbary', 'email': 'bary@ca.company.com'}}

你可以这样把它放到字典里:

store ={1:my_object, 2:my_object}

其中my_object是您创建的一个类,它包含您在问题中描述的属性:

url,owner email and commitMessage

我会使用元组作为数据,无需为其创建特殊类:

d = {1: (url, owner_mail, commit_msg), ...}

或者,如果索引实际上不太重要,则使用列表并隐式地拥有索引(数据在列表中的位置)。你知道吗

d = [(url, owner_mail, commit_msg), ...]

相关问题 更多 >