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 ...
随机推荐
- mvc framework ui component understand.
mvc: .htm是v, context和contex中的contextNode 是m, view controller, custom controller ,component contro ...
- 潜在风险的频次vs潜在风险的严重影响的程度(以及恢复)
潜在风险的频次vs潜在风险的严重影响的程度 海量数据的存储对于海量数据,不要存在这样的侥幸心理,一定要好好设计你的系统.把数据增长后存储的影响降到最低.面对海量数据,鸡肋的设计必然会导致系统的崩溃. ...
- linux 消息队列
消息队列,这个可是鼎鼎大名,经常在某些地方看见大家那个膜拜,那个,嗯,那个... 那就给个完整的例子,大家欣赏就行,我一直认为不用那个,嗯@ 这个队列的最大作用就是进程间通信,你要非搞个持久化,那也行 ...
- 68.jq---tab选项实现网页定点切换
{volist name="list" id="vo"}<div class="nav_div" style="positi ...
- 利用反射和JDBC元数据实现更加通用的查询方法
package com.at221.jdbc; import java.io.IOException; import java.io.InputStream; import java.sql.*; i ...
- parcel 在js中导入 html 文件
parcel不支持将html文件导入为字符串,如果您对parcel使用熟练,直接使用 parcel-plugin-phtml 插件即可,此插件使用 .phtml 后缀 为什么用parcel? 因为从我 ...
- (转)MERGE语法详解
merge语法是根据源表对目标表进行匹配查询,匹配成功时更新,不成功时插入. 其基本语法规则是 merge into 目标表 a using 源表 b on(a.条件字段1=b.条件字段1 and a ...
- spring boot 如何添加拦截
添加拦截其它挺简单的,直接上代码吧,我以简单的登陆验证拦截为例 1,先实现一个拦截器 package com.xinyue.interview.gm.filter; import javax.serv ...
- Unity用Vuforia做AR实现脱卡效果
这篇不错,记录下,博主不让转载 http://blog.csdn.net/qwe161819/article/details/76107105
- 无序数组求第k大/第k小的数
根据http://www.cnblogs.com/zhjp11/archive/2010/02/26/1674227.html 博客中所总结的7种解法,我挑了其中的解法3和解法6进行了实现. 解法3: ...