有了jmespath,处理python中的json数据就变成了一种享受…

有了jmespath,处理python中的json数据就变成了一种享受...

【阅读全文】

jmespath是python的第三方模块,是需要额外安装的。它在python原有的json数据处理上

做出了很大的贡献,至于效果如何,接下来试试看有多方便。

[En]

Made a great contribution, as for the effect next try to know how convenient it is.

不多说了,让我们开门见山吧。

[En]

Without saying much, let’s get straight to the point.

既然是第三方的库,那肯定是要安装的。通过pip的方式先将jmespath库安装好…

pip install jmespath

将安装的模块导入到代码块中。

[En]

Import the installed module into the code block.

import jmespath as jp

jmespath中有一个很重要、很方便的函数那就是search,不管你的json数据有多么变态,它都能给你找到。写出这个框架的大佬也是费心了…

我先准备了一个最简单的json数据结构的数据,数据层级为1。

json_data1 = {"name": "Python 集中营", "age": "10年"}

res = jp.search("name", json_data1)

print(res)

Python 集中营

如果是多级的json数据,可以使用key1.key2.key3的方式来获取value值。

json_data2 = {"names": {"name": "Python 集中营", "age": "5年"}}

res = jp.search("names.name", json_data2)

print(res)

Python 集中营

既然是针对json数据的处理,那肯定也是支持数组形式的查找的。对于json数据的数组形式的查找,主要是通过数组下标的方式来获取数据value值的。

json_data3 = ['Python 集中营', 'Sir.wang']

res = jp.search("[0]", json_data3)

print(res)

Python 集中营

若是数组和字典的形式结合组成的json数据,也可以组合使用查找方式。比如:有如下的json数据…

json_data4 = {
    "key1": {"key1_1": "value1_1"},
    "key2": {"key2_1": ["a", "b", "c"]}
}

面对稍微复杂一点的json数据可以先定义好表达式的字符串,最后再使用search函数进行数据查找。比如:需要在json数据中找到如下的数组…

["a", "b", "c"]

exp = "key2.key2_1[0]"

res = jp.search(exp, json_data4)

print("数组中的数值:", res)

数组中的数值:a

那么,如何在json数据中使用切片的方式来找到需要的json数据,当然也是支持的…

json_data5 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

exp = "[0:7]"

res = jp.search(exp, json_data5)

print("切片数据结果", res)

切片数据结果 [0, 1, 2, 3, 4, 5, 6]

还有一种查找方式,是通过*符号通配符的方式,主要是用在外层数据为数组的json数据中,比如有如下的数据…

json_data6 = {
    "data": [
        {"name": "Python 集中营", "age": "5年"},
        {"name": "Sir.wang", "age": "28"},
        {"dr": "nrg"}
    ]
}

这种数据形式的话,表达式可以这样写。先找到data作为键,这个时候是用data[*]就可以找到data下面的所有数据,在通过下一个层级的键进行匹配。

exp = "data[*].name"

res = jp.search(exp,json_data6)

print(res)

['Python 集中营', 'Sir.wang']

若是想匹配到的键是dr,则将exp表达式修改成下面这样就可以找到了…

exp = "data[*].dr"

搜索结果如下。<details><summary>*<font color='gray'>[En]</font>*</summary>*<font color='gray'>The result of the search is as follows.</font>*</details>

['nrg']

有了jmespath,处理python中的json数据就变成了一种享受...

【阅读全文】

解锁一个新技能,如何在Python代码中使用表情包…

万能的list列表,python中的堆栈、队列实现全靠它!

该怎么用pyqt5来实现数据的增、删、改、查功能…

介绍一个文字语音神器,几行代码就可以了!

[En]

Introduce a text voice artifact, a few lines of code can be done!

python批量自动整理文件

初学者福利:分享五个免费的 Python 学习网站,抓紧收藏吧!

有趣的控制台游戏:一行代码来绘制控制台图像!

[En]

Interesting console play: one line of code to draw the console image!

数据处理小工具:Excel 批量数据文件拆分/整合器…

办公自动化:PDF文件合并器,将多个PDF文件进行合并…

GUI猜数字游戏,直接开玩…

手把手教你做一个数据图表生成器(带源代码)。

[En]

Hand in hand to teach you to make a data chart generator (with source code).

动态指针时钟:利用pyqt5制作指针钟表显示实时时间

Original: https://www.cnblogs.com/lwsbc/p/16046819.html
Author: Python集中营
Title: 有了jmespath,处理python中的json数据就变成了一种享受…

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/509758/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球