分享一个 Linux 下的强力 Python 工具
2019年08月02日
作者:佚名
Linux 用户经常需要在终端查看一些数据,从文件里看或者网络协议获取数据并查看。比如,查看文件里的json数据;比如,查看 etcd里存下的数据。
如果直接看 cat或者curl得到的数据,如果格式乱掉了 会很痛苦的,而 Python 的json.tool可以在终端里把得到的数据格式化。
形如:cat json.file|python-m json.tool
用法及示例
# 终端操作 , vim json.file # 写入 如下内容:{ "code": 0,"data": "fine","error": "success" }
此时 cat json.file 看到的内容是 :
{ "code": 0,"data": "fine","error": "success" }
写进去啥样,就啥样!
此时用上这个工具试试
#终端执行 cat json.file | python -m json.tool # 看到的内容会变成这样: { "code": 0, "data": "fine", "error": "success" }
接下来再试试 etcd 的数据查看。
# 直接 curl 一下: curl localhost:2379/v2/keys # 拿到这个 {"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}]}} # 加上工具 curl localhost:2379/v2/keys |python -m json.tool # 拿到这个 { "action": "get", "node": { "dir": true, "nodes": [ { "createdIndex": 5, "dir": true, "key": "/NSQMetaData", "modifiedIndex": 5 }, { "createdIndex": 6726335, "dir": true, "key": "/b2c_systech_nsq", "modifiedIndex": 6726335 }, { "createdIndex": 4, "key": "/hello", "modifiedIndex": 4, "value": "world" } ] } }
可见,这个小工具,在终端环境下的帮助还是很大的,值得一学。