如何从subprocess.run json输出命令中提取值
我正在尝试将值分配给subprocess.run()命令的变量 执行上述命令后,下面我的代码下面
import subprocess
import_json_names = 'fileb://xxx/yyyy.json'
output = subprocess.run(['aws', 'apigateway', 'import-rest-api', '--body', f'{import_json_names}', '--profile', 'xxxx'],capture_output=True,
text=True, check=True)
finaloutput = output.stdout
print(finaloutput)
,我获得以下输出
{
"id": "123abc444",
"name": "xxxx",
"description": "xxx yy",
"createdDate": "2022-06-06T13:41:44+05:30",
"version": "2021-05-31T18:09:59Z",
"apiKeySource": "HEADER",
"endpointConfiguration": {
"types": [
"EDGE"
]
},
"disableExecuteApiEndpoint": false
}
,我想从上述输出中提取“ ID”值 如何实现同样的事情?
I am trying to assign the values to the variable from the subprocess.run() command
my code below
import subprocess
import_json_names = 'fileb://xxx/yyyy.json'
output = subprocess.run(['aws', 'apigateway', 'import-rest-api', '--body', f'{import_json_names}', '--profile', 'xxxx'],capture_output=True,
text=True, check=True)
finaloutput = output.stdout
print(finaloutput)
after execute the above command, I m getting the following output
{
"id": "123abc444",
"name": "xxxx",
"description": "xxx yy",
"createdDate": "2022-06-06T13:41:44+05:30",
"version": "2021-05-31T18:09:59Z",
"apiKeySource": "HEADER",
"endpointConfiguration": {
"types": [
"EDGE"
]
},
"disableExecuteApiEndpoint": false
}
I want to extract "id" values from the above output
how to achieve the same?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个例子,我只是回荡了一个JSON对象,但它也应该与您的子过程调用一起使用。 (我将额外的
shell
参数添加到为演示的运行
,但您可以为您的用例忽略它。)Here's an example, I just echo'd a JSON object but it should work with your subprocess call too. (I added the extra
shell
argument torun
for the demo but you can just ignore it for your use case.)