Issue
i'm new to python i need to use jq on an Json file and find match for "status" : "failed" in file, i had tried this in js code using this
jq -r '.' *.json | jq '(.steps) |= []' | grep -c '"status": "failed"'
it worked now i need to use it in jupyter notebook using below command it is not working giving syntax error,
import os
import json
import subprocess
cmd=['cd', 'Users/avinash/Downloads/reports/reports', '&&'','jq -r '.' *.json' | jq '(.steps) |= []' | grep -c "status": "failed"']
result = subprocess.check_output(cmd, shell=True)
print(result)
any help would be much appreciated, thanks i'm new to python
Solution
If you use shell=True
then you have to use single string instead of list
cmd = "cd Users/avinash/Downloads/reports/reports && jq -r '.' *.json | jq '(.steps) |= []' | grep -c '\"status\": \"failed\"'"
And rest is the same:
result = subprocess.check_output(cmd, shell=True)
print(result)
EDIT:
In original list you have two ''
after &&
and this makes first problem.
There are also other problems with '
in other parts.
But if you use |
then you have to use shell
and you don't need list.
But if you would have to use list then there is standard module shlex
to convert string to list
import shlex
cmd = "cd Users/avinash/Downloads/reports/reports && jq -r '.' *.json | jq '(.steps) |= []' | grep -c '\"status\": \"failed\"'"
print(shlex.split(cmd))
Result:
['cd', 'Users/avinash/Downloads/reports/reports', '&&', 'jq', '-r', '.', '*.json', '|', 'jq', '(.steps) |= []', '|', 'grep', '-c', '"status": "failed"']
(but |
will not work without shell=True
)
Answered By - furas
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.