通过可变数量的参数生成命令

2024-10-03 09:12:20 发布

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

我的方法接受两个属性和一个json列表,并返回相应的值。它按预期工作

现在,我想添加这个功能,让它返回相应的值,而不管它的深度如何

例如,现在它接受parentchild参数并返回一个值(见下文):return jsonprops[parent][child]

是否可以让它接受任意数量的参数并返回相应的值return jsonprops[parent][child1][child2]....[childN]

我发现了一个将变量数args传递给方法(如下)的示例,但我不确定如何构造return jsonprops[parent][child],因为它必须对[]中的参数进行排序

所需的解决方案将返回return jsonprops[parent][child]return jsonprops[parent][child1][child2][child3][child4]的值

将可变数量的参数传递给方法:

def multipleArgs(*arg):
    print "Called with", len(arg), "arguments:", arg

正在读取json文件:

import json

def read_json(parent, child, jsonprops=[])
    return jsonprops[parent][child]

exampleFile = json.loads(open(example.json).read())
childInfo = read_json('parentProps', 'childProp1', exampleFile)
print childInfo

示例json

{
  "generalInfo": {
    "name": "example",
    "schemaVersion": "1.0",
    "description": "metadata to be an example"
  },
  "parentProps": {
      "childProp1": "hi 1",
      "childProp2": "hi 2",
      "childProp3": {
        "newParent": [
          {
            "parent1": [
              {
                "grandChild1": "GC1",
                "grandChild2": "GC2",
                "grandChild3": "GC3"
              },
              {
                "numberofKids": "5",
                "grandChild4": "GC4",
                "grandChild5": "GC5",
                "grandChild6": "GC6"
              }
            ],
            "parent2": [
              {
                "numberofKids": "1",
                "grandChild11": "GC11",
                "grandChild12": "GC12",
                "grandChild13": "GC13"
              },
              {
                "grandChild14": "GC14",
                "grandChild15": "GC15",
                "grandChild16": "GC16"
              }
            ]
          }
        ]
      }
    }
  }

Tags: 方法jsonchild示例read参数数量return
2条回答

如何将jsonpathobjectpath模块一起使用:

In [22]: from objectpath import Tree

In [23]: j = """
    ...: {
    ...:   "generalInfo": {
    ...:   "name": "example",
    ...:   "schemaVersion": "1.0",
    ...:   "description": "metadata to be an example"
    ...:   },
    ...:   "parentProps": {
    ...:     "childProp1": "hi 1",
    ...:     "childProp2": "hi 2",
    ...:     "childProp3": {
    ...:       "newParent": [
    ...:         {
    ...:         "grandChild1": "GC1",
    ...:         "grandChild2": "GC2",
    ...:         "grandChild3": "GC3"
    ...:         },
    ...:         {
    ...:         "grandChild4": "GC4",
    ...:         "grandChild5": "GC5",
    ...:         "grandChild6": "GC6"
    ...:         }
    ...:       ]
    ...:     }
    ...:   }
    ...: }"""

In [24]: j = json.loads(j)

In [25]: next(Tree(j).execute('$..{}..{}'.format('parentProps', 'childProp1')))
Out[25]: 'hi 1'

In [53]: next(Tree(j).execute('$..{}..{}'.format('newParent', 'grandChild5')))
Out[53]: 'GC5'

然后,函数将加载json并在objectpath的帮助下返回结果(如果有)

要从数据结构中的任意深度访问值,可能需要使用循环。这里有一个简单的方法:

def get_value(data, *keys):
    for key in keys:
        data = data[key]
    return data

您还可以使用由reduce函数执行的隐式循环(在python3中,它是functools模块的一部分):

from functools import reduce # this line is only needed in Python 3
from operator import getitem

def get_value(*args): # data should be first argument, followed by any number of keys
    return reduce(getitem, args)

相关问题 更多 >