2016-09-20 13 views
1

ツイートのjson objcetからテキストフィールドを抽出し、それをsyntaxnetで実行します。私はPythonでそれをすべてやっています。シェルコマンドで変数を出力

私のコードは次のとおりです。parse機能で

import os, sys 
import subprocess 
import json 

def parse(text): 
    os.chdir("/var/www/html/alenza/hdfs/user/alenza/sree_account/sree_project/src/core/data_analysis/syntaxnet/models/syntaxnet") 
    #synnet_output = subprocess.check_output() 
    subprocess.call(["echo 'hello world' | syntaxet/demo.sh"], shell = True) 
    #print synnet_output 


for line in sys.stdin: 
    line1 = json.loads(line) 
    text = line1['avl_lexicon_text'] 
    print text 
    synnet_output = parse(text) 

、代わりのecho 'hello world'、私はそこtextをエコーし​​ます。つまり、text変数をsyntaxnet/demo.shファイルに送りたいと思っています。私はsubprocess.call(["echo text | syntaxet/demo.sh"], shell = True)をやってみたが、うまくいかなかった。どうやってやるの?

答えて

0

あなたは単なる文字列フォーマッタ%sを使用して、あなたのテキストは、単一引用符を持っていることが疑われる場合は、テキスト

def parse(text): 
    os.chdir("/var/www/html/alenza/hdfs/user/alenza/sree_account/sree_project/src/core/data_analysis/syntaxnet/models/syntaxnet") 
    #synnet_output = subprocess.check_output() 
    subprocess.call(["echo '%s' | syntaxet/demo.sh"%text], shell = True) 
    #print synnet_output 

を置き換えることができ、その後、あなたはそれを逃れることができます魔法のように

text = text.replace("'", "\\'") 
+0

作品。ありがとう! – kskp

+0

も参照してください。http://stackoverflow.com/a/39619413/874188 – tripleee

関連する問題