glom初级教程
1.glom介绍
通常对于字典和json的提取我们都是使用如下方式
>>> data = {'a': {'b': {'c': 'd'}}}
>>> data['a']['b']['c']
'd'
这种方式看起来简单,但是如果字段结构改变就引发了悲剧
>>> data2 = {'a': {'b': None}}
>>> data2['a']['b']['c']
Traceback (most recent call last):...
TypeError: 'NoneType' object is not subscriptable
- target: 需要提取的dict、json、list或者其他对象。
- spec: 我们想要的输出
>>> target = {'galaxy': {'system': {'planet': 'jupiter'}}}
>>> spec = 'galaxy.system.planet'
>>> glom(target, spec)
'jupiter'
2.glom安装
pip install glom from glom import *
3.基本路径提取
- 字符串
- Path对象
- T
>>> target = {'galaxy': {'system': {'planet': 'jupiter'}}}
>>> spec = 'galaxy.system.planet'
>>> glom(target, spec)
'jupiter'
现在数据结构换了,planet变成list了
>>> target = {'system': {'planets': [{'name': 'earth'}, {'name': 'jupiter'}]}}
>>> glom(target, ('system.planets', ['name']))
['earth', 'jupiter']
现在要求变了,数据加字段了,output需要多个字段 (多路径单一匹配)
>>> target = {'system': {'planets': [{'name': 'earth', 'moons': 1}, {'name': 'jupiter', 'moons': 69}]}}
>>> spec1 =('system.planets', ['name'])
>>> spec2 = ('system.planets', ['moons'])}
>>> pprint(glom(target, spec1))
['earth', 'jupiter']
>>> pprint(glom(target, spec2))
[1, 69]
这样写太麻烦了,glom提供了一个合并的方法,使用字典的方式格式化输出
>>> target = {'system': {'planets': [{'name': 'earth', 'moons': 1},{'name': 'jupiter', 'moons': 69}]}
>>> spec = {'names': ('system.planets', ['name']), 'moons': ('system.planets', ['moons'])}
>>> pprint(glom(target, spec))
{'moons': [1, 69], 'names': ['earth', 'jupiter']}
现在更复杂了,不仅多了字段,有的数据key也发生了变化 (多路径多匹配)
>>> target1 = {'system': {'dwarf_planets': [{'name': 'pluto', 'moons': 5},... {'name': 'ceres', 'moons': 0}]}}
>>> target2 = {'system': {'planets': [{'name': 'earth', 'moons': 1},... {'name': 'jupiter', 'moons': 69}]}}
>>> spec = {'names': (Coalesce('system.planets', 'system.dwarf_planets'), ['name']),'moons': (Coalesce('system.planets', 'system.dwarf_planets'), ['moons'])}
>>> pprint(glom(target, spec))
{'moons': [1, 69], 'names': ['earth', 'jupiter']}
>>> target = {'a': {'b': 'c', 'd.e': 'f', 2: 3}}
>>> glom(target, Path('a', 2))
3
>>> glom(target, Path('a', 'd.e'))
'f'
Path支持join
>>> Path(T['a'], T['b'])T['a']['b']
>>> Path(Path('a', 'b'),Path('c', 'd'))
Path('a', 'b', 'c', 'd')
Path支持切片
>>> path = Path('a', 'b', 1, 2)
>>> path[0]
Path('a')
>>> path[-2:]
Path(1, 2)
具体用法就是将字符串路径我位置替换成相应的Path对象
>>> spec = T['a']['b']['c']
>>> target = {'a': {'b': {'c': 'd'}}}
>>> glom(target, spec)
'd'
T提取出来的就是对应的python对象,(具体用法待考证)
>>> from glom import T
>>> target = {'system': {'planets': [{'name': 'earth', 'moons': 1},{'name': 'jupiter', 'moons': 69}]}
>>> spec = T['system']['planets'][-1].values()
>>> glom(target, spec)
['jupiter', 69] >>> spec = ('a', (T['b'].items(), list))
# reviewed below
>>> glom(target, spec)
[('c', 'd')]
>>> target = {'system': {'planets': [{'name': 'earth', 'moons': 1},{'name': 'jupiter', 'moons': 69}]}}
>>> pprint(glom(target, ('system.planets', ['moons'], sum)}))
70
>>> target = {'system': {'planets': [{'name': 'earth', 'moons': 1},{'name': 'jupiter', 'moons': 69}]}}
>>> pprint(glom(target, ('system.planets', ['moons'], [lambda x: x*2])}))
[2, 138]
>>> target = {'a': {'b': {}}}
>>> val = glom(target, Inspect('a.b'))
# wrapping a spec
---path: ['a.b']
target: {'a': {'b': {}}}
output: {}---
glom初级教程的更多相关文章
- Python图像处理库:Pillow 初级教程
Python图像处理库:Pillow 初级教程 2014-09-14 翻译 http://pillow.readthedocs.org/en/latest/handbook/tutorial.html ...
- shellKali Linux Web 渗透测试— 初级教程(第三课)
shellKali Linux Web 渗透测试— 初级教程(第三课) 文/玄魂 目录 shellKali Linux Web 渗透测试—初级教程(第三课) 课程目录 通过google hack寻找测 ...
- Mac OS X Terminal 101:终端使用初级教程
Mac OS X Terminal 101:终端使用初级教程 发表于 2012 年 7 月 29 日 由 Renfei Song | 文章目录 1 为什么要使用命令行/如何开启命令行? 2 初识Com ...
- Coding 初级教程(二)——上传已有项目
Coding 初级教程(二)——上传已有项目 [摘要:方针读者 :已具有 Coding.net 的账号. 本文首要先容若何把项目上传到 Coding.net 上. 分两种环境,一种是项目已归入到 gi ...
- [初级教程]用SecureCRT+Xming轻松远程实现Linux的X DISPLAY
[初级教程]用SecureCRT+Xming轻松远程实现Linux的X DISPLAY 发布者:sqqdugdu 时间:10-06 阅读数:2117 测试环境:RHEL 6.1,SecureCRT 5 ...
- Window服务初级教程以及log4net配置文件初始化
Window服务初级教程:http://www.jb51.net/article/48987.htm 另外,配置log4net这个日志功能的时候需要初始化,不然会报没有初始化的错误,而且初始化的节点应 ...
- 《自学C语言》初级教程 - 目录
我现在打算出一个C语言学习教程,目的是为了让初学者能够很容易和更深刻地理解C语言. 你可能有这样的疑问,网上不是有很多的初级教程吗,我需要这个吗?我的回答是:网上的C语言教程讲得不够全面,而且许多的初 ...
- Android初级教程理论知识(第三章测试&数据存储&界面展现)
首先介绍单元测试,我在javaweb部分有详细介绍单元测试框架的一篇文章. 可以先看在javaweb中的单元测试详解篇http://blog.csdn.net/qq_32059827/article/ ...
- Apache Solr 初级教程(介绍、安装部署、Java接口、中文分词)
Python爬虫视频教程零基础小白到scrapy爬虫高手-轻松入门 https://item.taobao.com/item.htm?spm=a1z38n.10677092.0.0.482434a6E ...
随机推荐
- 【Solution】API测试工具,访问方式
1. post的时候,@requestBody 用 json的传递方式
- 交叉编译7zip过程
https://github.com/Distrotech/p7zip.git 从这里下载,不要从sourceforge.net上下载,那上面的缺makefile文件. 我主要把文件 makefile ...
- php curl 上传json数据
PUT $data = array('username'=>'dog','password'=>'tall'); $data_json = json_encode($data); $ch ...
- Python之socketserver
import threading from socketserver import ThreadingTCPServer,BaseRequestHandler import sys import lo ...
- 【Qt官方MQTT库的使用,附一个MqttClient例子】
Qt官方MQTT库的使用,附一个MqttClient例子 开发环境:win7 64 + Qt5.9 记录时间:2018年3月11日 00:48:42 联系邮箱: yexiaopeng1992@126. ...
- 网络-01-端口号-linux端口详解大全
0 | 无效端口,通常用于分析操作系统1 | 传输控制协议端口服务多路开关选择器2 | 管理实用程序3 | 压缩进程5 | 远程作业登录7 | 回显9 | 丢弃11 | 在线用户13 | 时间17 | ...
- 【winform】serialPort 串口
一. 1.串口通信简单实现 该来的总会来的,学做硬件的,串口这个东西必须得门清. 俗话说的好,不会做串口助手的电子工程师不是好程序员.
- CCF CSP 201503-1 图像旋转 (降维)
题目链接:http://118.190.20.162/view.page?gpid=T27 问题描述 试题编号: 201503-1 试题名称: 图像旋转 时间限制: 5.0s 内存限制: 256.0M ...
- 对于react中rredux的理解
1.什么是redux? redux是一个应用数据流框架,主要作用是对于应用状态的管理 2.reducer特点 : (1)默认的state (2)state是只可读不可修改 (3)必须返回一个纯函数 3 ...
- windows杀进程
netstat -aon|findstr 1099 tasklist|findstr 1008 taskkill /pid 10084 -f