Xpath like query for nested python dictionaries

后端 未结 10 1660
情话喂你
情话喂你 2020-12-02 15:45

Is there a way to define a XPath type query for nested python dictionaries.

Something like this:

foo = {
  \'spam\':\'eggs\',
  \'morefoo\': {
               


        
10条回答
  •  情歌与酒
    2020-12-02 15:58

    There is an easier way to do this now.

    http://github.com/akesterson/dpath-python

    $ easy_install dpath
    >>> dpath.util.search(YOUR_DICTIONARY, "morefoo/morebar")
    

    ... done. Or if you don't like getting your results back in a view (merged dictionary that retains the paths), yield them instead:

    $ easy_install dpath
    >>> for (path, value) in dpath.util.search(YOUR_DICTIONARY, "morefoo/morebar", yielded=True)
    

    ... and done. 'value' will hold {'bacon': 'foobar'} in that case.

提交回复
热议问题