开发过程中访问接口时经常用到jq来过滤json,用着觉得不是很爽,于是自己搞一个舒服的 _

先说需求:

输入:参数1:被过滤对象(json、dict、list), 参数2:过滤路径

输出:过滤结果(python对象),默认格式化输出结果,key按字母顺序排列

支持过滤方式:

  • dict key过滤 .key

  • dict key列表 .keys()

  • dict value列表 .values()

  • dict key,value对 .iterms()

  • list过滤 .3 或 .[3]

  • list负索引 .-2 或 .[-2]

  • list切片1 .2:6 或 .[2:6]

  • list切片2 .2: 或 .[2:]

  • list切片3 .:6 或 .[:6]

  • list step1 .1:6:2 或 .[1:6:2]

  • list step2 .1::2 或 .[1::2]

  • list step3 .::2 或 .[::2]

  • string过滤..与list相同

  • string切片..与list相同

  • string 切片 step..与list相同

废话不多说,直接上核心代码,兼容Py2和Py3

from __future__ import unicode_literals

import json
import six def ppt(obj, path='.', with_print=True, normal_path_print=False):
base_string = str if six.PY3 else basestring
obj = json.loads(obj) if isinstance(obj, base_string) else obj
find_str, find_map = '', ['["%s"]', '[%s]', '%s', '.%s']
for im in path.split('.'):
if not im:
continue if isinstance(obj, (list, tuple, base_string)):
if im.startswith('[') and im.endswith(']'):
im = im[1:-1]
if ':' in im:
slice_default = [0, len(obj), 1]
obj, quota = obj[slice(
*[int(sli) if sli else slice_default[i] for i, sli in
enumerate(im.split(':'))])], 1
else:
obj, quota = obj[int(im)], 1
else:
if im in obj:
obj, quota = obj[im], 0
elif im.endswith('()'):
obj, quota = list(getattr(obj, im[:-2])()), 3
else:
if im.isdigit():
obj, quota = obj[int(im)], 1
else:
raise KeyError(im)
find_str += find_map[quota] % im if with_print:
print(obj if isinstance(obj, base_string) else
json.dumps(obj,
indent=4,
sort_keys=True,
ensure_ascii=False))
if normal_path_print:
print('get it normally with: <obj>%s' % find_str)
return obj

函数名:ppt,pretty print, 想不起更好的简短的命名了 ?_?

参数说明:

  • obj 输入的对象
  • path='.' 过滤字符串
  • with_print=True 是否格式化打印输出过滤结果
  • normal_path_print=False 是否输出过滤器反解后的正常查找方式

举例:

> test = '{"a": [1, 3, 4, 9, 10, 0, 5, 3, 7], "c": [{"h": 1, "d": [{"e": ["f", "g"]}]}], "b": "1234567890", "d": null}'
> ppt(test)
{
"a": [
1,
3,
4,
9,
10,
0,
5,
3,
7
],
"b": "1234567890",
"c": [
{
"d": [
{
"e": [
"f",
"g"
]
}
],
"h": 1
}
],
"d": null
}

上述输出key按字母顺序排序

> ppt(test, '.a.::2', normal_path_print=True)
[
1,
4,
10,
5,
7
]
get it normally with: <obj>["a"][::2]
> ppt(test, '.c.0.keys()', normal_path_print=True)
[
"h",
"d"
]
get it normally with: <obj>["c"][0].keys()

方便的地方:

如 一个复杂的引用数据的方式

['all_angles'][0]['nodes'][-1]['children'][1]['children'][3]['id']

换用更简单的方式,可以更简单快速的定位数据:

'all_angles.0.nodes.-1.children.1.children.3.id'

最新文章

  1. linux系统下sendmail的搭建
  2. Oracle数据库中SYS、SYSTEM、DBSNMP、SYSMAN四用户的区别
  3. 第 25 章 CSS3 过渡效果
  4. WEB前端开发和调试的工具
  5. C和CPP关于条件运算符的区别
  6. Java中栈结构的自我实现
  7. HDU 3255 Farming (线段树+扫面线,求体积并)
  8. JS实现一键复制功能
  9. JavaScript中依赖注入详细解析
  10. Android中使用&quot;running services&quot;查看service进程内存
  11. USB有时adb shell连不上设备
  12. js获取response头信息
  13. 莫烦scikit-learn学习自修第一天【scikit-learn安装】
  14. jboss eap6.1(1)
  15. vSphere虚拟化管理平台的功能
  16. java使用properties文件
  17. C语言:指针的几种形式
  18. PHP导出word,CVS,PDF
  19. python全栈开发_day10_函数的实参和形参
  20. git&#160;添加远程仓库后无法push

热门文章

  1. 【转】Spring 中三种Bean配置方式比较
  2. axios遇到的坑
  3. [.Net跨平台]部署DTCMS到Jexus遇到的问题及解决思路---部署
  4. Mac环境下实现alias重命名命令(永久生效)
  5. js 数组的常用方法归纳
  6. 写给自己的web总结——css篇(1)
  7. python变量字符拼接
  8. bzoj 3139: [Hnoi2013]比赛
  9. 使用JavaScript将图片保存至本地
  10. rabbitmq 启动报错