访问JSON对象中的嵌套键

2024-10-03 15:31:23 发布

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

我想在Python中访问一个特定的键值(“cote\u 1989\u base”)来修改它。但是我有一个类型错误:字符串索引必须是整数。你知道吗

我的代码:

import json

with open('demo_db_algolia_2019.json', encoding='utf-8') as data_file:
    data = json.loads(data_file.read())
    for x in data:
        cote1989base = x.get(["cote"][0]["cote_1989"]["cote_1989_eu"]["cote_1989_base"])
        print(cote1989base)

编辑:也许我解释得不好,因为我的整个JSON都在这样一个数组中:

    [{
    "objectID": 10035,
    "cote":
    {
        "cote_1989":
        {
            "cote_1989_f": "750000F",
            "cote_1989_eu":
            {                    
                "cote_1989_base": 190140                    
            }
        },
        "cote_2004":
        {                
            "cote_2004_base": 173320                
        },
        "cote_2014":
        {                
            "cote_2014_base": 420800                
        },
        "cote_2017":
        {                
            "cote_2017_base": 939600                
        },
        "cote_2019":
        {                
            "cote_2019_base": 939600                
        }
    }
},
    {
    "objectID": 10202,
    "cote":
    {
        "cote_1989":
        {
            "cote_1989_f": "27000F",
            "cote_1989_eu":
            {                    
                "cote_1989_base": 6844                    
            }
        },
        "cote_2004":
        {
            "cote_2004_base": 10894                
        },
        "cote_2014":
        {
            "cote_2014_base": 23670
        },
        "cote_2017":
        {                
            "cote_2017_base": 46980
        },
        "cote_2019":
        {                
            "cote_2019_base": 51156
        }
    }
}
]

它是否改变了一些主要问题?你知道吗


Tags: 字符串代码importjson类型database错误
3条回答

您应该按以下方式访问密钥:

x.get("cote").get("cote_1989").get("cote_1989_eu").get("cote_1989_base")

也可以使用以下函数简化代码:

def find(data, key):
    parts = key.split('/')
    dd = data
    for part in parts:
         if ( dd == None or not isinstance(dd,dict)):
            return None
         dd = dd.get(part)         
    return dd    

>>> data = { 'a' : { 'b' : { 'c' : { 'd':5} , 'c1':8 } }}
>>> find(data, 'a/b/c/d'))
5
>>> find(data, 'a/b/c2')
8
>>> find(data, 'z/b')
None

JSON中存在错误。大括号不匹配。用这个。你知道吗

import json


j = '''
{
    "objectID": 10035,
    "cote": {
        "cote_1989": {
            "cote_1989_f": "750000F",
            "cote_1989_eu": {
                "cote_1989_excp": 266196,
                "cote_1989_concours": 228168,
                "cote_1989_base": 190140,
                "cote_1989_be": 152112,
                "cote_1989_me": 114084,
                "cote_1989_ar": 76056,
                "cote_1989_epa": 38028
            }
        },
        "cote_2004": {
            "cote_2004_excp": 242648,
            "cote_2004_concours": 207984,
            "cote_2004_base": 173320,
            "cote_2004_be": 138656,
            "cote_2004_me": 103992,
            "cote_2004_ar": 69328,
            "cote_2004_epa": 34664
        }
    }
}'''

data = json.loads(j)
print(data['cote']['cote_1989']['cote_1989_eu']['cote_1989_base'])

输出:

190140

这个位:x.get(["cote"][0]["cote_1989"]肯定是错的。这里的方括号不是dict查找,而是一个列表文本,因此["cote"][0]的计算结果是"cote"。你知道吗

然后用["cote_1989"]给它下标,这就是你的TypeError的来源。你知道吗

您应该链接.get()调用:

x.get("cote").get("cote_1989").get("cote_1989_eu").get("cote_1989_base")

假设您的原始JSON是正确的,那么在您的原始代码中处理[0]位的方法就是“行噪声”。你知道吗

相关问题 更多 >