每個腳本都有自己的擅長。
有次實作一個work,使用了shell,php,python看着檔案種類多,不友善交接,看着也比較麻煩。
減少檔案種類數,也是很有必要的。
遇到的場景:shell程式需要從json中擷取資訊,繼續處理。
檔案, json.txt
{
"name": "中國",
"province": [{
"name": "黑龍江",
"cities": {
"city": ["哈爾濱", "大慶"]
}
}, {
"name": "廣東",
"cities": {
"city": ["廣州", "深圳", "珠海"]
}
}
]
}
可以在shell中直接調用python(注意load 與 loads的差別)
[email protected]:~/Desktop/test$ cat json.txt | /usr/bin/python2.7 -c "import json; import sys; obj=json.load(sys.stdin); print obj['province'][1]['name'].encode('utf-8')"
廣東
[email protected]:~/Desktop/test$ cat json.txt | /usr/bin/python2.7 -c "import json; import sys; obj=json.load(sys.stdin); print obj['province'][1]['cities']['city'][1].encode('utf-8')"
深圳
其他連結:
http://www.cnblogs.com/xudong-bupt/p/6218140.html
http://www.cnblogs.com/xudong-bupt/p/7291645.html