分享一個(gè) Linux 下的強(qiáng)力 Python 工具
Linux 用戶(hù)經(jīng)常需要在終端查看一些數(shù)據(jù),從文件里看或者網(wǎng)絡(luò)協(xié)議獲取數(shù)據(jù)并查看。比如,查看文件里的json數(shù)據(jù);比如,查看 etcd里存下的數(shù)據(jù)。
如果直接看 cat或者curl得到的數(shù)據(jù),如果格式亂掉了 會(huì)很痛苦的,而 Python 的json.tool可以在終端里把得到的數(shù)據(jù)格式化。
形如:cat json.file|python-m json.tool
用法及示例
- # 終端操作 ,
- vim json.file
- # 寫(xiě)入 如下內(nèi)容:{ "code": 0,"data": "fine","error": "success" }
此時(shí) cat json.file 看到的內(nèi)容是 :
- { "code": 0,"data": "fine","error": "success" }
寫(xiě)進(jìn)去啥樣,就啥樣!
此時(shí)用上這個(gè)工具試試
- #終端執(zhí)行
- cat json.file | python -m json.tool
- # 看到的內(nèi)容會(huì)變成這樣:
- {
- "code": 0,
- "data": "fine",
- "error": "success"
- }
接下來(lái)再試試 etcd 的數(shù)據(jù)查看。
- # 直接 curl 一下:
- curl localhost:2379/v2/keys
- # 拿到這個(gè)
- {"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
- # 拿到這個(gè)
- {
- "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"
- }
- ]
- }
- }
可見(jiàn),這個(gè)小工具,在終端環(huán)境下的幫助還是很大的,值得一學(xué)。