社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

分享一个 Linux 下的强力 Python 工具

编程派 • 5 年前 • 542 次点击  


文 | 名白 @cnblog

编辑 | EarlGrey 

推荐 | 编程派公众号(ID:codingpy)

Linux 用户经常需要在终端查看一些数据,从文件里看或者网络协议获取数据并查看。比如,查看文件里的json数据;比如,查看 etcd 里存下的数据。

如果直接看 cat 或者 curl 得到的数据,如果格式乱掉了 会很痛苦的,而 Python 的 json.tool 可以在终端里把得到的数据格式化。

形如:cat json.file|python-m json.tool

用法及示例

  1. # 终端操作 ,


  2. vim json.file


  3. # 写入 如下内容:{ "code": 0,"data": "fine","error": "success" }

此时 cat json.file 看到的内容是 :

  1. { "code": 0,"data": "fine","error": "success" }

写进去啥样,就啥样!

此时用上这个工具试试

  1. #终端执行

  2. cat json.file | python -m json.tool


  3. # 看到的内容会变成这样:


  4. {

  5. "code": 0,

  6. "data": "fine",

  7. "error": "success"

  8. }

接下来再试试 etcd 的数据查看。

  1. # 直接 curl 一下:

  2. curl localhost:2379/v2/keys


  3. # 拿到这个

  4. {"action":"get","node":{"dir":true,"nodes":[{"key":"/NSQMetaData","dir":true,"modifiedIndex":5,"createdIndex":5 },{"key":"/b2c_systech_nsq","dir":true,"modifiedIndex":6726335,"createdIndex":6726335},{"key":"/hello","value":"world","modifiedIndex":4,"createdIndex":4}]}}


  5. # 加上工具


  6. curl localhost:2379/v2/keys |python -m json.tool


  7. # 拿到这个


  8. {

  9. "action": "get",

  10. "node": {

  11. "dir": true,

  12. "nodes": [

  13. {

  14. "createdIndex": 5,

  15. "dir": true,

  16. "key": "/NSQMetaData",

  17. "modifiedIndex": 5

  18. },

  19. {

  20. "createdIndex": 6726335,

  21. "dir": true,

  22. "key": "/b2c_systech_nsq",

  23. "modifiedIndex": 6726335

  24. },

  25. {

  26. "createdIndex": 4,

  27. "key": "/hello",

  28. "modifiedIndex": 4,

  29. "value": "world"

  30. }

  31. ]

  32. }

  33. }

可见,这个小工具,在终端环境下的帮助还是很大的,值得一学。

原文:https://www.cnblogs.com/mingbai/p/linuxPyJson.html

回复下方「关键词」,获取优质资源


回复关键词「 pybook03」,立即获取主页君与小伙伴一起翻译的《Think Python 2e》电子版

回复关键词「pybooks02」,立即获取 O'Reilly 出版社推出的免费 Python 相关电子书合集

回复关键词「书单02」,立即获取主页君整理的 10 本 Python 入门书的电子版



豆瓣 9.1 分,中文版销量 30 多万,零基础也能用这本书学会 Python

你想要的 IT 电子资源,这里可能都有


Python 或将超越 C、Java,成为最受欢迎的语言

Python 容器使用的 5 个技巧和 2 个误区

如何写出优雅的 Python 函数?

题图:pexels,CC0 授权。

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/36921
 
542 次点击