首页 > 解决方案 > Python - 查找具有匹配值的对象

问题描述

我从如下所示的 API 中检索了 JSON:

"entries": [
    {
        "cuid": "1234",
        "name": "23423423",
        "id": "5432",
    },
    {
        "cuid": "12234",
        "name": "123124",
        "id": "5432",
    },

我正在尝试遍历一个单独的名称列表并从匹配的对象中检索相应的 ID。

我使用json.loads将 JSON 存储在users_dict我试图从中检索值的位置。

本质上,假设我有 name = "23423423" ,我如何在 users_dict 中找到具有该名称的对象并从中检索 "5432" 的 id。

我怎样才能完成这项任务?

标签: python-3.x

解决方案


以下是我使用 CSV 作为列表的解决方案:

with open('test.csv') as f:
reader = csv.reader(f)
for row in reader: 
    user = (' '.join(row))
    for i , val  in enumerate(users_dict):
        if (val["name"] == user):
            deletion_list.append(val["id"])

推荐阅读