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 ...
随机推荐
- python 图像处理,画一个正弦函数
import numpy as np from PIL import Image import matplotlib.pyplot as plt import math size = 300 new_ ...
- reduce函数
python中的reduce python中的reduce内建函数是一个二元操作函数,他用来将一个数据集合(链表,元组等)中的所有数据进行下列操作:用传给reduce中的函数 func()(必须是 ...
- 命令行下执行python找不包的解决方法
首先我们来了解一下,为什么会出现这样的问题,以及python搜索包的机制是怎么样的 1.为什么会出现这样的问题? 包是向下搜索机制. 2.为什么ide中执行没有报找不到包的问题? python搜索机制 ...
- 计算机网络网络层的IP地址划分及子码
现在在网络层,即就是TCP/IP协议里的网际互联层,最流行IP协议的就是IPV4.其中IP地址的格式是由32位二进制数字表示的,通常为了人们阅读习惯,将其转换成点分十进制来表示,如:192.168.1 ...
- Android Glide 加载图片
0.借鉴文章地址:http://blog.csdn.net/zivensonice/article/details/51835802 和 http://www.cnblogs.com/zhaoyanj ...
- redis 简单的注册
首页写一个页面 <form method="post" action="hreg.php"> Name:<input type="t ...
- Mac 下 实现终端跳转 服务器 不用输入密码
首先需要安装 expect 安装 expect 需要 tcl 依赖 第一步 下载tcl http://www.tcl.tk/software/tcltk/downloadnow84.tml 将下载好 ...
- [ABP] ASP.NET Zero 5.6.0 之 破解日志
继上次ASP.NET Zero 5.5.2的破解https://www.cnblogs.com/VAllen/p/ABP-ASP-NET-Zero-5-5-2-Crack.html之后,现在发布了AS ...
- go语言入门教程:基本语法之变量声明及注意事项
一.变量的使用 1.1 什么是变量 变量是为存储特定类型的值而提供给内存位置的名称.在go中声明变量有多种语法. 所以变量的本质就是一小块内存,用于存储数据,在程序运行过程中数值可以改变 1.2 声明 ...
- c# 将CSV文件转成list集合
//定义CSV文件所对应的实体类 public class example { public int t1; public string t2; public string t3; public st ...