溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發(fā)布時間:2021-10-28 17:08:50 來源:億速云 閱讀:1365 作者:柒染 欄目:編程語言

本篇文章給大家分享的是有關Linux下的強力Python工具json.tool怎么用,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

Linux 用戶經(jīng)常需要在終端查看一些數(shù)據(jù),從文件里看或者網(wǎng)絡協(xié)議獲取數(shù)據(jù)并查看。比如,查看文件里的json數(shù)據(jù);比如,查看  etcd里存下的數(shù)據(jù)。

如果直接看 cat或者curl得到的數(shù)據(jù),如果格式亂掉了 會很痛苦的,而 Python 的json.tool可以在終端里把得到的數(shù)據(jù)格式化。

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

用法及示例

# 終端操作 , vim json.file  # 寫入 如下內(nèi)容:{ "code": 0,"data": "fine","error": "success" }

此時 cat json.file 看到的內(nèi)容是 :

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

寫進去啥樣,就啥樣!

此時用上這個工具試試

#終端執(zhí)行 cat json.file | python -m json.tool # 看到的內(nèi)容會變成這樣: { "code": 0, "data": "fine", "error": "success"  }

接下來再試試 etcd 的數(shù)據(jù)查看。

# 直接 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" } ] } }

可見,這個小工具,在終端環(huán)境下的幫助還是很大的,值得一學。

以上就是Linux下的強力Python工具json.tool怎么用,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學到更多知識。更多詳情敬請關注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI