2016-09-20 10 views
1

Ich möchte das Textfeld aus einem JSON Objcet eines Tweets extrahieren und es durch Syntaxnet ausführen. Ich mache alles in Python.Druckvariable in Shell-Befehl

Mein Code ist:

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) 

Statt nun echo 'hello world' in parse Funktion möchte ich text dort ein Echo. Das heißt, ich möchte die text Variable Datei zu füttern. Ich habe versucht, subprocess.call(["echo text | syntaxet/demo.sh"], shell = True) zu tun, aber das hat nicht funktioniert. Wie kann ich das machen?

Antwort

0

Sie können nur %s String Formatierer verwenden und ersetzen den Text

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 

Wenn Sie vermuten, dass Ihr Text Apostrophe haben, dann können Sie es entkommen:

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

Arbeiten wie ein Charme. Vielen Dank! – kskp

+0

Siehe auch http://StackOverflow.com/a/39619413/874188 – tripleee

Verwandte Themen