Shell - New line seperator in python script output - python

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"

Related

Bash/Python: SyntaxError: unexpected character after line continuation character whilst piping to python

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.

How can I specify quotes(" ") in my string text in Python?

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"

Python If Statement Error, in bash_profile function

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 ''"

Python - all the print and stdout how can i get from terminal so that i can make a log?

Why it does not give output while doing from bash >>, so that it can be saved to a file.
$ cat > /var/tmp/runme.sh << \EOF
#!/bin/bash
export DISPLAY=:0.0
python /var/tmp/t.py >> /var/tmp/log.log &
sleep 3
ps aux | grep "t.py" | grep -v "grep" | awk '{print $2}' | xargs kill -9;
EOF
$ cat > /var/tmp/t.py << \EOF
import sys
print "[RAN]: OK"
sys.stdout.write("[RAN]: OK")
sys.stdout.flush()
EOF
$ chmod +x /var/tmp/runme.sh ; /var/tmp/runme.sh &
$ cat /var/tmp/log.log
$ tail -f /var/tmp/log.log
^
Showing nothing.
How can i get the outputs to log.log using Bash and Python combination?
you could have python do:
var = "hello world"
f = open('log.log', 'r+')
print(var)
f.write(var)
Within var/tmp/t.py, do something similar to the following:
var = "[RAN]: OK"
f = open("log.log", "a+")
f.write(var)
print var
f.close()
Opening "log.log" with the "a+" argument will allow you to append the output of t.py to the file so that you can keep track of multiple runs of t.py. If you just want the output for the most recent run you could just use "w+" which will create a new file if one does not exist and rewrite the file if it does.
You could also put a time stamp on each log using datetime like so:
import datetime
now = datetime.datetime.now()
var = "Date: " + now.strftime("%Y-%m-%d %H:%M") + "\n" + "[RAN]: OK"
f = open("log.log", "a+")
f.write(var)
print var
f.close()
I don't know if this script is something you will run periodically in which knowing the date the log was created would be useful or whether it is a one and done script but I thought you may find date logging useful.

Redirect bash output to python script

I am using zbarimg to scan bar codes, I want to redirect the output to a python script. How can I redirect the output of the following command:
zbarimg code.png
to a python script, and what should be the script like?
I tried the following script:
#!/usr/local/bin/python
s = raw_input()
print s
I made it an executable by issuing the following:
chmod +x in.py
Than I ran the following :
zbarimg code.png | in.py
I know it's wrong but I can't figure out anything else!
Use sys.stdin to read from stdin in your python script. For example:
import sys
data = sys.stdin.readlines()
Using the pipe operator | from the command is correct, actually. Did it not work?
You might need to explicitly specify the path for the python script as in
zbarimg code.png | ./in.py
and as #dogbane says, reading from stdin like sys.stdin.readlines() is better than using raw_input
I had to invoke the python program command as
somecommand | python mypythonscript.py instead of somecommand | ./mypythonscript.py. This worked for me. The latter produced errors.
My purpose: Sum up the durations of all mp3 files by piping output of soxi -D *mp3
into python: soxi -D *mp3 | python sum_durations.py
Details:
soxi -D *mp3produces:
122.473016
139.533016
128.456009
307.802993
...
sum_durations.py script:
import sys
import math
data = sys.stdin.readlines()
#print(data)
sum = 0.0
for line in data:
#print(line)
sum += float(line)
mins = math.floor(sum / 60)
secs = math.floor(sum) % 60
print("total duration: " + str(mins) + ":" + str(secs))

Categories