2017-07-20 23 views
-1

こんにちは私は、子プロセス(nodejs)を介してPythonスクリプトを実行しており、sys.stdinによって与えられた配列をループすることができません。Pythonでsys.stdin配列をループする

私はすべての解決方法を試しましたが、私にとってはうまくいかないようです。

しかし、別のファイルで配列の結果を実行しようとしたときに正常に動作しています。 sample test code -

の下body.customerに示すようcrawler.py

`

import sys, json, numpy as np 

print "==> in crawler python" 
def main(): 

    print "-----"; 
    count = 0 

    for line in sys.stdin: 
     print line 

     print "printed line" 
     count += 1; 
    print type(sys.stdin.readlines()); 



    print count; 



    print "=====" 
    sys.stdout.flush(); 

if __name__ == '__main__': 
    main() 

`

出力---子プロセスを呼び出しノードファイルJSONの配列であります

var process = SPAWN('python', [PATH.join(__dirname,"../crawler.py")]), 
        data = body.customers; 
        //dataString = ''; 

       process.stdout.on('error', function (err) { 
        console.log('stdout error: ', err); 
       }); 

       process.stdout.on('data', function(data){ 
        console.log(data.toString()) 
        //dataString += data.toString(); 
       }); 
       process.stdout.on('end', function(){ 
        console.log("end") 
        //console.log('Sum of numbers=',dataString); 
       }); 
       process.stdin.write(JSON.stringify(data)); 
       process.stdin.end(); 


    body.customer = [ { 
     "id":4609083345, 
     "email":"[email protected]", 
     "accepts_marketing":True, 
     "created_at":"2016-11-18T17:42:58-05:00", 
     "updated_at":"2016-11-23T23:54:35-05:00", 
     "first_name":"Testing", 
     "last_name":"McTester", 
     "orders_count":3, 
     "state":"disabled", 
     "total_spent":"0.00", 
     "last_order_id":4394012817, 
     "note":None, 
     "verified_email":True, 
     "multipass_identifier":None, 
     "tax_exempt":False, 
     "phone":None, 
     "tags":"", 
     "last_order_name":"#157932", 
     "addresses":[ 
     { 
      "id":4906471441, 
      "customer_id":4609083345, 
      "first_name":"Testing", 
      "last_name":"McTester", 
      "company":"", 
      "address1":"123 Test St", 
      "address2":"", 
      "city":"Testtown", 
      "province":"California", 
      "country":"United States", 
      "zip":"90210", 
      "phone":"14169999999", 
      "name":"Testing McTester", 
      "province_code":"CA", 
      "country_code":"US", 
      "country_name":"United States", 
      "default":False 
     }, 
     { 
      "id":4906753937, 
      "customer_id":4609083345, 
      "first_name":"Tester", 
      "last_name":"McTestington", 
      "company":"", 
      "address1":"123 Test St", 
      "address2":"", 
      "city":"Testtown", 
      "province":"California", 
      "country":"United States", 
      "zip":"90210", 
      "phone":"4169999999", 
      "name":"Tester McTestington", 
      "province_code":"CA", 
      "country_code":"US", 
      "country_name":"United States", 
      "default":True 
     } 
     ], 
     "default_address":{ 
     "id":4906753937, 
     "customer_id":4609083345, 
     "first_name":"Tester", 
     "last_name":"McTestington", 
     "company":"", 
     "address1":"123 Test St", 
     "address2":"", 
     "city":"Testtown", 
     "province":"California", 
     "country":"United States", 
     "zip":"90210", 
     "phone":"4169999999", 
     "name":"Tester McTestington", 
     "province_code":"CA", 
     "country_code":"US", 
     "country_name":"United States", 
     "default":True 
     } 
    }, { 
     "id":4609083345, 
     "email":"[email protected]", 
     "accepts_marketing":True, 
     "created_at":"2016-11-18T17:42:58-05:00", 
     "updated_at":"2016-11-23T23:54:35-05:00", 
     "first_name":"Testing", 
     "last_name":"McTester", 
     "orders_count":3, 
     "state":"disabled", 
     "total_spent":"0.00", 
     "last_order_id":4394012817, 
     "note":None, 
     "verified_email":True, 
     "multipass_identifier":None, 
     "tax_exempt":False, 
     "phone":None, 
     "tags":"", 
     "last_order_name":"#157932", 
     "addresses":[ 
     { 
      "id":4906471441, 
      "customer_id":4609083345, 
      "first_name":"Testing", 
      "last_name":"McTester", 
      "company":"", 
      "address1":"123 Test St", 
      "address2":"", 
      "city":"Testtown", 
      "province":"California", 
      "country":"United States", 
      "zip":"90210", 
      "phone":"14169999999", 
      "name":"Testing McTester", 
      "province_code":"CA", 
      "country_code":"US", 
      "country_name":"United States", 
      "default":False 
     }, 
     { 
      "id":4906753937, 
      "customer_id":4609083345, 
      "first_name":"Tester", 
      "last_name":"McTestington", 
      "company":"", 
      "address1":"123 Test St", 
      "address2":"", 
      "city":"Testtown", 
      "province":"California", 
      "country":"United States", 
      "zip":"90210", 
      "phone":"4169999999", 
      "name":"Tester McTestington", 
      "province_code":"CA", 
      "country_code":"US", 
      "country_name":"United States", 
      "default":True 
     } 
     ], 
     "default_address":{ 
     "id":4906753937, 
     "customer_id":4609083345, 
     "first_name":"Tester", 
     "last_name":"McTestington", 
     "company":"", 
     "address1":"123 Test St", 
     "address2":"", 
     "city":"Testtown", 
     "province":"California", 
     "country":"United States", 
     "zip":"90210", 
     "phone":"4169999999", 
     "name":"Tester McTestington", 
     "province_code":"CA", 
     "country_code":"US", 
     "country_name":"United States", 
     "default":True 
     } 
    }] 

yのヘルプをいただければ幸いです。私はスタックオーバーフローのソリューションのほとんどを見渡しましたが、うまく動作していないようです。 助けていただければ幸いです。 Thankx

+0

? – jacoblaw

+0

stdinは配列ではありません。また、 'numpy'インポートの目的は何ですか? –

+0

@jacoblaw - タイプミスで申し訳ありません。 – Kushan

答えて

0

sys.stdinを繰り返すことで、JSONにはまったく関係ない個々の行が得られます。あなたはそうのように、stdinのすべてを読み込むと、実際のリストにJSON文字列から、それをデコードします:

import json 
data = json.load(sys.stdin) 
`印刷line`は、それがあるべきよりも多くのインデントされている理由
+0

その作業をするか、 'sys.stdin.read()'が必要ですか? –

+0

@ juanpa.arrivillaga 'load'(' loads'とは対照的に)はファイルのためのもので、 'read()'を行います。 –

+0

もちろん!私は慎重に読んでいませんでした... –

関連する問題