Python 如何使用列表路径获取json值

Python 如何使用列表路径获取json值,python,json,list,Python,Json,List,大家好,我下面有一个json文本 } 我创建了一个函数,可以获取json所有字段的路径。 如下图所示: def get_paths(source): paths = [] if isinstance(source, collections.MutableMapping): # found a dict-like structure... for k, v in source.items(): # iterate over it; Python 2.x: source.iteritem

大家好,我下面有一个json文本

}

我创建了一个函数,可以获取json所有字段的路径。 如下图所示:

def get_paths(source):
paths = []
if isinstance(source, collections.MutableMapping):  # found a dict-like structure...
    for k, v in source.items():  # iterate over it; Python 2.x: source.iteritems()
        paths.append([k])  # add the current child path
        paths += [[k] + x for x in get_paths(v)]  # get sub-paths, extend with the current
# else, check if a list-like structure, remove if you don't want list paths included
elif isinstance(source, collections.Sequence) and not isinstance(source, str):
    #                          Python 2.x: use basestring instead of str ^
    for i, v in enumerate(source):
        paths.append([i])
        paths += [[i] + x for x in get_paths(v)]  # get sub-paths, extend with the current
return paths
现在您可以看到如下所示的所有路径:

def get_paths(source):
paths = []
if isinstance(source, collections.MutableMapping):  # found a dict-like structure...
    for k, v in source.items():  # iterate over it; Python 2.x: source.iteritems()
        paths.append([k])  # add the current child path
        paths += [[k] + x for x in get_paths(v)]  # get sub-paths, extend with the current
# else, check if a list-like structure, remove if you don't want list paths included
elif isinstance(source, collections.Sequence) and not isinstance(source, str):
    #                          Python 2.x: use basestring instead of str ^
    for i, v in enumerate(source):
        paths.append([i])
        paths += [[i] + x for x in get_paths(v)]  # get sub-paths, extend with the current
return paths
如何遍历路径列表以获取json每个字段的值?

Python提供了一个模块,可以将json文件读入Python dict数据结构:

import json

with open("my_file.json", "r") as my_file:
    contents = json.loads(my_file.read())
之后,您可以将其视为一个dict:

现在,假设您有一个这样的列表,您在问题中创建了该列表:

lk = ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'Acronym']
以下是一个可用于检索相关值的函数:

def retrieve_value(key_list, dct):
    subdict = dct
    for k in key_list:
        subdict = subdict[k]
    return subdict

retrieve_value(lk, contents)
Python提供了模块,该模块可以将JSON文件读入Python dict数据结构:

import json

with open("my_file.json", "r") as my_file:
    contents = json.loads(my_file.read())
之后,您可以将其视为一个dict:

现在,假设您有一个这样的列表,您在问题中创建了该列表:

lk = ['glossary', 'GlossDiv', 'GlossList', 'GlossEntry', 'Acronym']
以下是一个可用于检索相关值的函数:

def retrieve_value(key_list, dct):
    subdict = dct
    for k in key_list:
        subdict = subdict[k]
    return subdict

retrieve_value(lk, contents)

我建议您使用json库。 您可以使用json.loads将json转换为dict,您可以通过dict_name[key]访问值


在编写函数之前,请尝试查找库。

我建议您使用json库。 您可以使用json.loads将json转换为dict,您可以通过dict_name[key]访问值


在编写函数之前,请尝试查找库。

有一个内置的json库,您可以使用json.loads从文件或字符串变量加载json。请参阅“有一个json内置库”,您可以使用json.loads从文件或字符串变量加载json。如果您正在打开一个物理文件,请参阅。使用contents=json.loadmy\u文件可能更简单。。但结果相同。您好,谢谢您的评论,但有些东西不是我想要的,当前问题是我获取json字段路径标题A=['glossary','GlossDiv','title']我如何从数据[A[0]][A[1][A[2]]?@andylei我编辑了我的答案,加入了一个部分,介绍如何从问题底部的列表中获取信息嗨,绿色斗篷,兄弟,我找到你了非常感谢:嗨,兄弟,如果我使用lk路径更新内容,怎么做?示例使用lk update content set only缩写值已更改?如果要打开物理文件,使用contents=json.loadmy_文件可能更简单。。但结果相同。您好,谢谢您的评论,但有些东西不是我想要的,当前问题是我获取json字段路径标题A=['glossary','GlossDiv','title']我如何从数据[A[0]][A[1][A[2]]?@andylei我编辑了我的答案,加入了一个部分,介绍如何从问题底部的列表中获取信息嗨,绿色斗篷,兄弟,我找到你了非常感谢:嗨,兄弟,如果我使用lk路径更新内容,怎么做?示例使用lk更新内容集仅首字母缩略词值更改?Hi simone感谢您评论我的错误,因为问题不清楚,当前问题其我获取json字段路径标题A=['glossary',GlossDiv',title']如何从数据[A[0]][A[1]][A[2]]获取倾斜值?Hi simone感谢您评论我的错误,因为问题不清楚,当前问题its i get json字段路径标题A=['glossary','GlossDiv','title']如何从数据[A[0]][A[1][A[2]]中获取倾斜值?