I am very new to python and am trying to use it to parse a file with JSON in it within my bash_profile script, and return the value. I can't seem to get the if statement working. It will read the contents of the file, but I recieve this error, with an arrow under the if statement.
File "<string>", line 1
import sys,json;data=json.loads(sys.stdin.read());if data['123456789']:print...
^
SyntaxError: invalid syntax
Contents of File:
{"123456789":"Well Hello"}
Function that searches file:
function find_it {
file=~/.pt_branches
CURRENT_CONTENTS=`cat $file`
echo $CURRENT_CONTENTS | python -c "import sys,json;data=json.loads(sys.stdin.read());if data['$1']:print data['$1'];else:print '';"
}
Command
find_it "123456789"
If I were to remove the if statement and just return the value, it does work, but I want it to be able to return and empty string if it does not exist.
echo $CURRENT_CONTENTS | python -c "import sys,json;data=json.loads(sys.stdin.read());print data['$1'];"
Any ideas on what I am doing wrong?!?
Code golf!
"import sys, json; data=json.loads(sys.stdin.read()); print data.get('$1') or ''"
Do this:
echo $CURRENT_CONTENTS | python -c "
import sys
import json
data = json.loads(sys.stdin.read())
if data['$1']:
print data['$1']
else:
print ''"
Related
How do I specify a new line in python output? Because the code below is not working.
Already tried 'os.linesep'.
LOGS=$(echo "$RESPONSE" | python -c "
import sys, json, os;
res=json.load(sys.stdin);
result=''
for x in range(len(res['hits']['hits'])):
result += res['hits']['hits'][x]['_source']['message'] + '\n'
print result
")
echo -e $LOGS
The problem is that you need to echo the variable $LOG in quotes to expand any carriage returns. The last line should therefore be:
echo -e "$LOGS"
Attempt to call python function from scala fails with below error. But works fine when the same command is invoked directly from command line.
Please find below simplified code snippets :-
greeting.py
import logging
import os
def greet(arg):
print("hey " + arg)
StraightPyCall.scala
package git_log
object StraightPyCall {
def main(args: Array[String]): Unit = {
val commandWithNewLineInBeginning =
"""
|python -c "import sys;sys.path.append('~/playground/octagon/bucket/pythonCheck'); from greeting import *; greet('John')"
|""".stripMargin
//new line stripped out from beginning and end
val executableCommand = commandWithNewLineInBeginning.substring(1, commandWithNewLineInBeginning.length - 1)
println("command is :-")
println(executableCommand)
import sys.process._
s"$executableCommand".!!
}
}
output of above scala program is :-
command is :-
python -c "import sys;sys.path.append('~/playground/octagon/bucket/pythonCheck'); from greeting import *; greet('John')"
File "<string>", line 1
"import
^
SyntaxError: EOL while scanning string literal
Exception in thread "main" java.lang.RuntimeException: Nonzero exit value: 1
at scala.sys.package$.error(package.scala:26)
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.slurp(ProcessBuilderImpl.scala:134)
at scala.sys.process.ProcessBuilderImpl$AbstractBuilder.$bang$bang(ProcessBuilderImpl.scala:104)
at git_log.StraightPyCall$.main(StraightPyCall.scala:19)
at git_log.StraightPyCall.main(StraightPyCall.scala)
When I tried executing the command that is printed on the console. It works perfectly fine.
python -c "import sys;sys.path.append('~/playground/octagon/bucket/pythonCheck'); from greeting import *; greet('John')"
Result:-
hey John
Note : Below is the ProcessBuilder toString representation (copied from stacktrace while debugging) :-
[python, -c, "import, sys;sys.path.append('/Users/mogli/jgit/code-conf/otherScripts/pythonScripts/CallRelativePyFromBash/pyscripts');, from, Greet, import, *;, greet_with_arg('John')"]
Kindly suggest, what needs to be modified in commandWithNewLineInBeginning to make it work from scala
It works from the command line because the shell is parsing and interpreting the string before invoking the python command. In the Scala code the ProcessBuilder is trying to parse and interpret the string without the shell's help.
We can help the interpreting. This should work.
Seq("python"
,"-c"
,"import sys;sys.path.append('~/playground/octagon/bucket/pythonCheck'); from greeting import *; greet('John')"
).!!
If you really have to start out with the full string then maybe you can break it up before processing.
For example: If you know that the pattern is always "cmnd -c string" then this might work.
commandWithNewLineInBeginning.replaceAll("\"","")
.split("((?=-c)|(?<=-c))")
.map(_.trim)
.toSeq.!!
I am trying to pipe into python using bash:
#!/bin/sh
echo "from rest_framework_api_key.models import APIKey\n_, key = APIKey.objects.create_key(name='test_key')\nprint(key)" | python manage.py shell
This gives the following error:
from rest_framework_api_key.models import APIKey\n_, key = APIKey.objects.create_key(name='test_key')\nprint(key)
^
SyntaxError: unexpected character after line continuation character
Strangely, this works:
#!/bin/sh
echo "from users.models import CustomUser\nCustomUser.objects.create_superuser('foo', '$1')" | python manage.py shell
And I can't see the difference in terms of new lines / escaping / quoting. If I remove the pipe to python (i.e. it runs only the echo command), it does not error.
bash's echo doesn't expand \n to a newline by default. Use printf:
printf "from rest_framework_api_key.models import APIKey\n_, key = APIKey.objects.create_key(name='test_key')\nprint(key)" | python manage.py shell
or more simply, a here document:
python manage.py shell << EOF
from rest_framework_api_key.models import APIKey
_, key = APIKey.objects.create_key(name='test_key')
print(key)
EOF
You are missing the -e flag in the echo.
It should be :
echo -e "from rest_framework_api_key.models import APIKey\n_, key = APIKey.objects.create_key(name='test_key')\nprint(key)"
Let me know if it helps!
Note : I only now noticed the shebang in your code. Unless you have a very specific reason not to do so, you should change your #!/bin/sh for #!/bin/bash.
The command I want to run from my python script is:
tool-manager-cli -a "somexml.xml" -c "another.xml"
My python code is:
command1 = """ tool-manager-cli -a "somexml.xml" -c "another.xml" """
subprocess.Popen(command1)
However when I run my py script, I get SyntaxError: invalid syntax as my error for this line.
How can I specify quotes(" ") in my string text without closing it or invalid syntax?
Try using single quotes to differentiate like so
command1 = """ tool-manager-cli -a 'somexml.xml' -c 'another.xml' """
You can use format method to structure your command:
command1= "{} {} {} {} {}".format("tool-managel-cli","-a", "somexml.xml","-c","another.xml")
subprocess.Popen(command1)
You can also concat by + , but its tedious ofcourse
command1 ="tool-manager-cli"+" -a"+ " somexml.xml" + " -c "+" another.xml"
I have the following code in bash script
python -u - << EOF
from my.package import script
script.run()
EOF
>> /path/to/log/file
In script.py there are a number of print statements and I would like to redirect that output from the console to a file. The file gets created successfully, but it's empty.
How can I go about this? What am I missing here?
The idea is right, but the way you re-direct the output to a file from here-document is wrong, Heredocs themselves re-directs just like any other commands, just do
python -u - << EOF >> /path/to/log/file
from my.package import script
script.run()
EOF
try this,
import sys
f = open("log_file.txt", 'w')
sys.stdout = f
print "My print statements"
f.close()
Right syntax is:
python -u - >> /path/to/log/file << EOF
from my.package import script
script.run()
EOF
The reason is that if you write in bash something like that:
util_1 | util_2 | util_3 >> some_file < another_file
or
util_1 | util_2 | util_3 >> some_file << EOF
...
EOF
another_file or here-document goes to the standard input of the first utility in pipeline (in this case to util_1).