Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python命令行中将这个json字符串作为param编写?_Python_Json - Fatal编程技术网

如何在python命令行中将这个json字符串作为param编写?

如何在python命令行中将这个json字符串作为param编写?,python,json,Python,Json,我正在通过python脚本编写qemu qga命令,如下所示: python cmd.py request:'{"execute": "guest-execute", "session": "4768", "arguments": {"commands": [{"command": "/bin/sh", "arguments": ["sh", "-c", "route | grep default | awk '{print $2}'"]}]}}' 但是param被解析为bellow,它从{p

我正在通过python脚本编写qemu qga命令,如下所示:

python cmd.py request:'{"execute": "guest-execute", "session": "4768", "arguments": {"commands": [{"command": "/bin/sh", "arguments": ["sh", "-c", "route | grep default | awk '{print $2}'"]}]}}'
但是param被解析为bellow,它从{print$2}中分离json字符串:

['cmd.py', 'request:{"execute": "guest-execute", "session": "4768", "arguments": {"commands": [{"command": "/bin/sh", "arguments": ["sh", "-c", "route | grep default | awk {print', '}"]}]}}']
实际上我想要这个,哪个awk{print'{print$2}是在vm中运行的shell命令,并且此格式无法更改:

['cmd.py', 'request:{"execute": "guest-execute", "session": "4768", "arguments": {"commands": [{"command": "/bin/sh", "arguments": ["sh", "-c", "route | grep default | awk {print'{print $2}'}"]}]}}']
可能吗?如果不可能,这意味着我必须在cmd.py中手工构造json字符串


谢谢~

您意外地在其他单引号中嵌入了单引号: 如果省略字符串的第一部分,则得到以下结果:

请求:“…|awk”{print$2}]}]}”

壳牌将其视为三部分,并试图解决其中的2美元问题:

请求:'…|awk'{print$2}']}]}

如果要在其他单引号中嵌入单引号,必须按以下方式执行:

python cmd.py request:'{"execute": "guest-execute", "session": "4768", "arguments": {"commands": [{"command": "/bin/sh", "arguments": ["sh", "-c", "route | grep default | awk '{print $2}'"]}]}}'

echo“此字符串包含”\您必须删除空格并对引号使用反斜杠。这样,整个json字符串将被视为一个参数。