使用词典:

d = {'1': 'a'}

如何使用Python中的JMESPath库提取值a

以下try 没有成功:

import jmespath

value = jmespath.search("1", d) 
# throws jmespath.exceptions.ParseError: invalid token
value = jmespath.search("'1'", d) 
# returns the key '1' instead of the value 'a'

推荐答案

如果标识符是unquoted-string(即A-Za-z0-9_)以及字符串以A-Za-z_开头,则可以直接地址.

From the grammar rule listed above identifiers can be one or more characters, and must start with A-Za-z_.
An identifier can also be quoted. This is necessary when an identifier has characters not specified in the unquoted-string grammar rule. In this situation, an identifier is specified with a double quote, followed by any number of unescaped-char or escaped-char characters, followed by a double quote.

Source: JMESPath documentation, 100

由于您的标识符是0-9,因此您必须对此标识符使用双引号字符串,因此为"1".

因此,您的工作Python代码是:

import jmespath

d = {'1': 'a'}

value = jmespath.search('"1"', d)

print(value)

Python相关问答推荐

是否有方法将现有的X-Y图转换为X-Y-Y1图(以重新填充)?

我可以使用极点优化这个面向cpu的pandas代码吗?

单击Python中的复选框后抓取数据

Twilio:CallInstance对象没有来自_的属性'

使文本输入中的文本与标签中的文本相同

Chatgpt API不断返回错误:404未能从API获取响应

使用mySQL的SQlalchemy过滤重叠时间段

将特定列信息移动到当前行下的新行

如何比较numPy数组中的两个图像以获取它们不同的像素

运行Python脚本时,用作命令行参数的SON文本

为什么带有dropna=False的groupby会阻止后续的MultiIndex.dropna()工作?

如何从数据库上传数据到html?

在含噪声的3D点网格中识别4连通点模式

如何根据一列的值有条件地 Select 前N组?

pandas在第1列的id,第2列的标题,第3列的值,第3列的值?

如何从需要点击/切换的网页中提取表格?

需要帮助重新调整python fill_between与数据点

Django admin Csrf令牌未设置

基于Scipy插值法的三次样条系数

为什么在FastAPI中创建与数据库的连接时需要使用生成器?