標籤:php load 令行 json code 直接 shell命令 python2 執行
每個指令碼都有自己的擅長。
有次實現一個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
shell命令列執行python