gpt4 book ai didi

python-3.x - 如何使用 starts_with jmespath.search 字典键

转载 作者:行者123 更新时间:2023-12-05 04:02:19 26 4
gpt4 key购买 nike

我有一本字典,我想在字典的键上做一个嵌套的 jmespath.search 以查找以特定字符串开头的键,但我似乎只能使用一次 @ 运算符.

> d = {'foo1': 'bar', 'foo2' : 'baz'}  # here's a dummy example

> jmespath.search('keys(@)[?starts_with(@, "foo")]', jmespath.search('@', d)) # in an ideal world, I'd get ['foo1', 'foo2']



> *** jmespath.exceptions.JMESPathTypeError: In function starts_with(), invalid type for value: None, expected one of: ['string'], received: "null"

这是我实际得到的,但是当我输入以下内容时:

> jmespath.search('keys(@)[?starts_with([@], "foo")]', jmespath.search('@', d))  # in an ideal world, I'd get ['foo1', 'foo2'] 

我明白了

> *** jmespath.exceptions.JMESPathTypeError: In function starts_with(), invalid type for value: ['foo1'], expected one of: ['string'], received: "array"

在 JMESPath 中有没有办法做到这一点,还是我在做梦?我需要嵌套位

最佳答案

烦人的是,答案是查询中的单引号和双引号需要调换...

jmespath.search("keys(@)[?starts_with(@, 'foo')]", jmespath.search('@', d))

有效。

关于python-3.x - 如何使用 starts_with jmespath.search 字典键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54431449/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com