温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linux下的强力Python工具json.tool怎么用

发布时间:2021-10-28 17:08:50 来源:亿速云 阅读:1345 作者:柒染 栏目:编程语言

本篇文章给大家分享的是有关Linux下的强力Python工具json.tool怎么用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

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" } ] } }

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

以上就是Linux下的强力Python工具json.tool怎么用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI