shell命令列執行python

來源:互聯網
上載者:User

標籤: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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.