I wrote a small shell script that looks like this:
cd models/syntaxnet
var1=$(jq --raw-output '.["avl_text"]' | syntaxnet/demo.sh)
echo $var1
python /home/sree/python_code1.py $var1
My python_code1.py
looks like this:
import sys
data = sys.argv[1]
print "In python code"
print data
print type(data)
Now, the output of echo $var1 in my shell script is exactly what I wanted to see:
1 Check _ VERB VB _ 0 ROOT _ _ 2 out _ PRT RP _ 1 prt _ _ 3 this _ DET DT _ 4 det _ _ 4 video _ NOUN NN _ 1 dobj _ _ 5 about _ ADP IN _ 4 prep _ _ 6 Northwest _ NOUN NNP _ 7 nn _ _ 7 Arkansas _ NOUN NNP _ 5 pobj _ _ 8 - _ . , _ 7 punct _ _ 9 https _ X ADD _ 7 appos _ _
But the output of print data
in the python code is just 1
. i.e. the first letter of the argument.
Why is this happening? I want to pass the entire string to the python code.
If there is space in between argument and argument is not in quotes, then python consider as two different arguments.
That's why the output of print data in the python code is just 1.
Check the below output.
[root@dsp-centos ~]# python dsp.py Dinesh Pundkar
In python code
Dinesh
[root@dsp-centos ~]# python dsp.py "Dinesh Pundkar"
In python code
Dinesh Pundkar
[root@dsp-centos ~]#
So, in your shell script, put $var1 in quotes.
Content of shell script(a.sh):
var1="Dinesh Pundkar"
python dsp.py "$var1"
Content of python code(dsp.py):
import sys
data = sys.argv[1]
print "In python code"
print data
Output:
[root@dsp-centos ~]# sh a.sh
In python code
Dinesh Pundkar
Use Join and list slicing
import sys
data = ' '.join(sys.argv[1:])
print "In python code"
print data
print type(data)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With