码迷,mamicode.com
首页 > 编程语言 > 详细

Python 之glom的多种数据套嵌处理

时间:2018-11-07 19:58:34      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:name   方法   font   col   python   plane   mes   pytho   count   

glom的数据处理

glom广泛地介绍了日常数据和对象操作,帮助您开始着眼于编写健壮的声明性数据转换每个应用程序都处理数据,而现在,即使是最简单的应用程序也会处理丰富的,大量嵌套的数据

1.字典套字典

#目的获取字典c的数据

jvmdata={a: {b: {c: d}}}

print("打印结果是",glom(jvmdata,‘a.b.c‘))

打印结果是:d

target = {‘galaxy‘: {‘system‘: {‘planet‘: ‘jupiter‘}}}
spec = ‘galaxy.system.planet‘
print(glom(target, spec))

打印结果:jupiter
 

 

2. 字典套列表获取数据

target = {system: {planets: [{name: earth}, {name: jupiter}]}}
spec =system.planets
print(glom(target,(spec,[name])))

[‘earth‘, ‘jupiter‘]

 

3.类似数据重组

字典套列表套字典

需求:获取names和moons的数据后重新组成字典对应列表的格式

换个角度思考 target是orm获取的数据需要重新组成新的数据

 

target = {system: {planets: [{name: earth, moons: 1},
                              {name: jupiter, moons: 69}]}}

spec={"names":(system.planets, [name]),
      "moons":(system.planets, [moons])}
print(glom(target,spec))

{moons: [1, 69], names: [earth, jupiter]}

 

4.glom的可以使用一些方法和函数 sum 求和

target = {system: {planets: [{name: earth, moons: 1},
                                  {name: jupiter, moons: 69}]}}
 
print glom(target, {moon_count: (system.planets, [moons], sum)})

# {‘moon_count‘: 70}

 

写的不好,还望会的同学不吝赐教啊

Python 之glom的多种数据套嵌处理

标签:name   方法   font   col   python   plane   mes   pytho   count   

原文地址:https://www.cnblogs.com/yingfei/p/9924636.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!