如何解析字典列表

时间:2014-12-03 05:27:31

标签: python list dictionary

我从Web服务获得以下字典列表:

[{"user_id": null, "name": "image", "resource_id": "2e81fbe8-6a03-4796-9d8c-c003c1e6e561", "source": "openstack", "meter_id": "MmU4MWZiZTgtNmEwMy00Nzk2LTlkOGMtYzAwM2MxZTZlNTYxK2ltYWdl\n", "project_id": "a9be8c1d04b94632860737075e58d094", "type": "gauge", "unit": "image"}, {"user_id": null, "name": "image", "resource_id": "49d640c0-2bbc-4496-b6c3-965fd441d1f2", "source": "openstack", "meter_id": "NDlkNjQwYzAtMmJiYy00NDk2LWI2YzMtOTY1ZmQ0NDFkMWYyK2ltYWdl\n", "project_id": "a9be8c1d04b94632860737075e58d094", "type": "gauge", "unit": "image"}, {"user_id": null, "name": "image", "resource_id": "7702315c-4c74-41b9-b641-2dc694b7efdf", "source": "openstack", "meter_id": "NzcwMjMxNWMtNGM3NC00MWI5LWI2NDEtMmRjNjk0YjdlZmRmK2ltYWdl\n", "project_id": "f8bd7d5659a74eb6a6c47650a43511b5", "type": "gauge", "unit": "image"}, {"user_id": null, "name": "image", "resource_id": "827de420-aa8e-4294-9e5b-954a0c181a43", "source": "openstack", "meter_id": "ODI3ZGU0MjAtYWE4ZS00Mjk0LTllNWItOTU0YTBjMTgxYTQzK2ltYWdl\n", "project_id": "a9be8c1d04b94632860737075e58d094", "type": "gauge", "unit": "image"}, {"user_id": null, "name": "image", "resource_id": "9ad7dd3c-d754-4614-9ba4-e5e6dd93eab6", "source": "openstack", "meter_id": "OWFkN2RkM2MtZDc1NC00NjE0LTliYTQtZTVlNmRkOTNlYWI2K2ltYWdl\n", "project_id": "f8bd7d5659a74eb6a6c47650a43511b5", "type": "gauge", "unit": "image"}, {"user_id": null, "name": "image", "resource_id": "bbea2145-b30d-4ded-8f39-ace6761e6235", "source": "openstack", "meter_id": "YmJlYTIxNDUtYjMwZC00ZGVkLThmMzktYWNlNjc2MWU2MjM1K2ltYWdl\n", "project_id": "a9be8c1d04b94632860737075e58d094", "type": "gauge", "unit": "image"}, {"user_id": null, "name": "image", "resource_id": "bcedf078-4d0b-4d6d-b7d2-b3c698e810ac", "source": "openstack", "meter_id": "YmNlZGYwNzgtNGQwYi00ZDZkLWI3ZDItYjNjNjk4ZTgxMGFjK2ltYWdl\n", "project_id": "f8bd7d5659a74eb6a6c47650a43511b5", "type": "gauge", "unit": "image"}, {"user_id": null, "name": "image", "resource_id": "cbaa1562-c6c0-4922-b5ec-95ae71d0ebda", "source": "openstack", "meter_id": "Y2JhYTE1NjItYzZjMC00OTIyLWI1ZWMtOTVhZTcxZDBlYmRhK2ltYWdl\n", "project_id": "f8bd7d5659a74eb6a6c47650a43511b5", "type": "gauge", "unit": "image"}, {"user_id": null, "name": "image.upload", "resource_id": "2e81fbe8-6a03-4796-9d8c-c003c1e6e561", "source": "openstack", "meter_id": "MmU4MWZiZTgtNmEwMy00Nzk2LTlkOGMtYzAwM2MxZTZlNTYxK2ltYWdlLnVwbG9hZA==\n", "project_id": "a9be8c1d04b94632860737075e58d094", "type": "delta", "unit": "image"}, {"user_id": null, "name": "image.upload", "resource_id": "49d640c0-2bbc-4496-b6c3-965fd441d1f2", "source": "openstack", "meter_id": "NDlkNjQwYzAtMmJiYy00NDk2LWI2YzMtOTY1ZmQ0NDFkMWYyK2ltYWdlLnVwbG9hZA==\n", "project_id": "a9be8c1d04b94632860737075e58d094", "type": "delta", "unit": "image"}, {"user_id": null, "name": "image.upload", "resource_id": "7702315c-4c74-41b9-b641-2dc694b7efdf", "source": "openstack", "meter_id": "NzcwMjMxNWMtNGM3NC00MWI5LWI2NDEtMmRjNjk0YjdlZmRmK2ltYWdlLnVwbG9hZA==\n", "project_id": "f8bd7d5659a74eb6a6c47650a43511b5", "type": "delta", "unit": "image"}, {"user_id": null, "name": "image.upload", "resource_id": "827de420-aa8e-4294-9e5b-954a0c181a43", "source": "openstack", "meter_id": "ODI3ZGU0MjAtYWE4ZS00Mjk0LTllNWItOTU0YTBjMTgxYTQzK2ltYWdlLnVwbG9hZA==\n", "project_id": "a9be8c1d04b94632860737075e58d094", "type": "delta", "unit": "image"}, {"user_id": null, "name": "image.upload", "resource_id": "9ad7dd3c-d754-4614-9ba4-e5e6dd93eab6", "source": "openstack", "meter_id": "OWFkN2RkM2MtZDc1NC00NjE0LTliYTQtZTVlNmRkOTNlYWI2K2ltYWdlLnVwbG9hZA==\n", "project_id": "f8bd7d5659a74eb6a6c47650a43511b5", "type": "delta", "unit": "image"}, {"user_id": null, "name": "image.upload", "resource_id": "bbea2145-b30d-4ded-8f39-ace6761e6235", "source": "openstack", "meter_id": "YmJlYTIxNDUtYjMwZC00ZGVkLThmMzktYWNlNjc2MWU2MjM1K2ltYWdlLnVwbG9hZA==\n", "project_id": "a9be8c1d04b94632860737075e58d094", "type": "delta", "unit": "image"}, {"user_id": null, "name": "image.upload", "resource_id": "bcedf078-4d0b-4d6d-b7d2-b3c698e810ac", "source": "openstack", "meter_id": "YmNlZGYwNzgtNGQwYi00ZDZkLWI3ZDItYjNjNjk4ZTgxMGFjK2ltYWdlLnVwbG9hZA==\n", "project_id": "f8bd7d5659a74eb6a6c47650a43511b5", "type": "delta", "unit": "image"}, {"user_id": null, "name": "image.upload", "resource_id": "cbaa1562-c6c0-4922-b5ec-95ae71d0ebda", "source": "openstack", "meter_id": "Y2JhYTE1NjItYzZjMC00OTIyLWI1ZWMtOTVhZTcxZDBlYmRhK2ltYWdlLnVwbG9hZA==\n", "project_id": "f8bd7d5659a74eb6a6c47650a43511b5", "type": "delta", "unit": "image"}, {"user_id": null, "name": "image.update", "resource_id": "2e81fbe8-6a03-4796-9d8c-c003c1e6e561", "source": "openstack", "meter_id": "MmU4MWZiZTgtNmEwMy00Nzk2LTlkOGMtYzAwM2MxZTZlNTYxK2ltYWdlLnVwZGF0ZQ==\n", "project_id": "a9be8c1d04b94632860737075e58d094", "type": "delta", "unit": "image"}, {"user_id": null, "name": "image.update", "resource_id": "49d640c0-2bbc-4496-b6c3-965fd441d1f2", "source": "openstack", "meter_id": "NDlkNjQwYzAtMmJiYy00NDk2LWI2YzMtOTY1ZmQ0NDFkMWYyK2ltYWdlLnVwZGF0ZQ==\n", "project_id": "a9be8c1d04b94632860737075e58d094", "type": "delta", "unit": "image"}, {"user_id": null, "name": "image.update", "resource_id": "7702315c-4c74-41b9-b641-2dc694b7efdf", "source": "openstack", "meter_id": "NzcwMjMxNWMtNGM3NC00MWI5LWI2NDEtMmRjNjk0YjdlZmRmK2ltYWdlLnVwZGF0ZQ==\n", "project_id": "f8bd7d5659a74eb6a6c47650a43511b5", "type": "delta", "unit": "image"}, {"user_id": null, "name": "image.update", "resource_id": "827de420-aa8e-4294-9e5b-954a0c181a43", "source": "openstack", "meter_id": "ODI3ZGU0MjAtYWE4ZS00Mjk0LTllNWItOTU0YTBjMTgxYTQzK2ltYWdlLnVwZGF0ZQ==\n", "project_id": "a9be8c1d04b94632860737075e58d094", "type": "delta", "unit": "image"}, {"user_id": null, "name": "image.update", "resource_id": "9ad7dd3c-d754-4614-9ba4-e5e6dd93eab6", "source": "openstack", "meter_id": "OWFkN2RkM2MtZDc1NC00NjE0LTliYTQtZTVlNmRkOTNlYWI2K2ltYWdlLnVwZGF0ZQ==\n", "project_id": "f8bd7d5659a74eb6a6c47650a43511b5", "type": "delta", "unit": "image"}, {"user_id": null, "name": "image.update", "resource_id": "bbea2145-b30d-4ded-8f39-ace6761e6235", "source": "openstack", "meter_id": "YmJlYTIxNDUtYjMwZC00ZGVkLThmMzktYWNlNjc2MWU2MjM1K2ltYWdlLnVwZGF0ZQ==\n", "project_id": "a9be8c1d04b94632860737075e58d094", "type": "delta", "unit": "image"}, {"user_id": null, "name": "image.update", "resource_id": "bcedf078-4d0b-4d6d-b7d2-b3c698e810ac", "source": "openstack", "meter_id": "YmNlZGYwNzgtNGQwYi00ZDZkLWI3ZDItYjNjNjk4ZTgxMGFjK2ltYWdlLnVwZGF0ZQ==\n", "project_id": "f8bd7d5659a74eb6a6c47650a43511b5", "type": "delta", "unit": "image"}, {"user_id": null, "name": "image.update", "resource_id": "cbaa1562-c6c0-4922-b5ec-95ae71d0ebda", "source": "openstack", "meter_id": "Y2JhYTE1NjItYzZjMC00OTIyLWI1ZWMtOTVhZTcxZDBlYmRhK2ltYWdlLnVwZGF0ZQ==\n", "project_id": "f8bd7d5659a74eb6a6c47650a43511b5", "type": "delta", "unit": "image"}, {"user_id": null, "name": "image.size", "resource_id": "2e81fbe8-6a03-4796-9d8c-c003c1e6e561", "source": "openstack", "meter_id": "MmU4MWZiZTgtNmEwMy00Nzk2LTlkOGMtYzAwM2MxZTZlNTYxK2ltYWdlLnNpemU=\n", "project_id": "a9be8c1d04b94632860737075e58d094", "type": "gauge", "unit": "B"}, {"user_id": null, "name": "image.size", "resource_id": "49d640c0-2bbc-4496-b6c3-965fd441d1f2", "source": "openstack", "meter_id": "NDlkNjQwYzAtMmJiYy00NDk2LWI2YzMtOTY1ZmQ0NDFkMWYyK2ltYWdlLnNpemU=\n", "project_id": "a9be8c1d04b94632860737075e58d094", "type": "gauge", "unit": "B"}, {"user_id": null, "name": "image.size", "resource_id": "7702315c-4c74-41b9-b641-2dc694b7efdf", "source": "openstack", "meter_id": "NzcwMjMxNWMtNGM3NC00MWI5LWI2NDEtMmRjNjk0YjdlZmRmK2ltYWdlLnNpemU=\n", "project_id": "f8bd7d5659a74eb6a6c47650a43511b5", "type": "gauge", "unit": "B"}, {"user_id": null, "name": "image.size", "resource_id": "827de420-aa8e-4294-9e5b-954a0c181a43", "source": "openstack", "meter_id": "ODI3ZGU0MjAtYWE4ZS00Mjk0LTllNWItOTU0YTBjMTgxYTQzK2ltYWdlLnNpemU=\n", "project_id": "a9be8c1d04b94632860737075e58d094", "type": "gauge", "unit": "B"}, {"user_id": null, "name": "image.size", "resource_id": "9ad7dd3c-d754-4614-9ba4-e5e6dd93eab6", "source": "openstack", "meter_id": "OWFkN2RkM2MtZDc1NC00NjE0LTliYTQtZTVlNmRkOTNlYWI2K2ltYWdlLnNpemU=\n", "project_id": "f8bd7d5659a74eb6a6c47650a43511b5", "type": "gauge", "unit": "B"}, {"user_id": null, "name": "image.size", "resource_id": "bbea2145-b30d-4ded-8f39-ace6761e6235", "source": "openstack", "meter_id": "YmJlYTIxNDUtYjMwZC00ZGVkLThmMzktYWNlNjc2MWU2MjM1K2ltYWdlLnNpemU=\n", "project_id": "a9be8c1d04b94632860737075e58d094", "type": "gauge", "unit": "B"}, {"user_id": null, "name": "image.size", "resource_id": "bcedf078-4d0b-4d6d-b7d2-b3c698e810ac", "source": "openstack", "meter_id": "YmNlZGYwNzgtNGQwYi00ZDZkLWI3ZDItYjNjNjk4ZTgxMGFjK2ltYWdlLnNpemU=\n", "project_id": "f8bd7d5659a74eb6a6c47650a43511b5", "type": "gauge", "unit": "B"}, {"user_id": null, "name": "image.size", "resource_id": "cbaa1562-c6c0-4922-b5ec-95ae71d0ebda", "source": "openstack", "meter_id": "Y2JhYTE1NjItYzZjMC00OTIyLWI1ZWMtOTVhZTcxZDBlYmRhK2ltYWdlLnNpemU=\n", "project_id": "f8bd7d5659a74eb6a6c47650a43511b5", "type": "gauge", "unit": "B"}]

我想: 1)将此列表解析为单独的词典 2)从每个字典中提取值

因此,伪代码将是:

for listItem in my_list:
    userId = listItem["user_id"]
    name = listItem["name"]
    resourceId = listItem["resource_id"]

实现此目的的python语法是什么?

2 个答案:

答案 0 :(得分:1)

也许这个简单的例子是说明性的:

from pprint import pprint
import json

json_string = '[{"name": "Jean Luc", "rank": "Captain"}, {"name": "Worf", "rank": "Commander"}]'

officers = json.loads(json_string)
for officer in officers:
    pprint(officer)   

您希望查看Python的json库[1],将JSON从字符串/文件解析为正常的数据结构,如列表和dicts。

[1] https://docs.python.org/2/library/json.html

答案 1 :(得分:0)

listItem[0]["user_id"]
listItem[0]["name"]
listItem[0]["resource_id"]

类似于下一个数组

listItem[1]["user_id"]
listItem[1]["name"]
listItem[1]["resource_id"]

并使用

检查数组的长度
len(listItem)