微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何使用Jmespath查询复杂的嵌套字典?

如何解决如何使用Jmespath查询复杂的嵌套字典?

鉴于以下是我从yaml文件转换而来的复杂的嵌套字典,我想查询row_1row_2键的值。

我正在使用库jmespath来帮助对此进行查询

这是我尝试过的:

import jmespath
import pprint

data =  [ 
            { 'includes': 
                            [ 
                                { 
                                    'style': 
                                            [ 
                                                'css/module/sandBox/style.css','css/modules/sandBox/mobile.css'
                                            ]
                                },{ 'javascript': 
                                            [ 
                                                'js/module/sandBox/script.js','js/module/sandBox/mobile.js'
                                            ]
                                 }
                            ]
            },{ 'row_1': 
                            [ 
                                {
                                    'classes': 'my-3 text-light text-center bg-primary'
                                },{ 
                                    'columns': 
                                            [ 
                                                { 
                                                    'col_1': 
                                                            [ 
                                                                {'size': 3},{ 'classes': 'my-3 text-light ' 'text-center ' 'bg-primary'}
                                                            ]
                                                },{ 
                                                    'col_2': 
                                                            [ 
                                                                {'size': 3},{ 'classes': 'my-3 text-light ' 'text-center ' 'bg-primary'},{ 'styles': 
                                                                    [ 
                                                                        { 
                                                                            'div.summary_credit_score': 
                                                                                [ 
                                                                                    { 
                                                                                        'font': '300 ' '12px ' 'open-san,' 'arial,' 'Time ' 'New ''Romance'
                                                                                    }
                                                                                ]
                                                                        }
                                                                    ]
                                                                }
                                                            ]
                                                 }
                                            ]
                                }
                            ]
            },{ 'row_2': 
                            [ 
                                {
                                    'classes': 'my-3 text-light text-center bg-primary'
                                },' 'Time ' 'New ''Romance'
                                                                                    }
                                                                                ]
                                                                        }
                                                                    ]
                                                                }
                                                            ]
                                                 }
                                            ]
                                }
                            ]
            }
    ]

# get row
rows = jmespath.search('[*].row_*',data)[0] # not success

pp = pprint.PrettyPrinter(indent=2)
pp.pprint(rows)

我上面的查询旨在获取字典的值,该字典的键的键以row_开头,但不会成功。谢谢。

解决方法

据我所知,在使用 jmespath 时,不能在键名中包含通配符,只能用于处理值。

最好的办法是使用字典理解。

rows = {k:v for data_dict in data for k,v in data_dict.items() if 'row' in k}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。