简体   繁体   中英

How to get output data from shell redirection inside a Python script?

Actually, I am creating several utilities to deal with AWS CLI responses, which responds with json. My whole scripts ecosystem are based in Python, and usually I create tiny utilities that fetches the json data and parses to readable/organized data.

But I have noticed that I am taking so much time to creates scripts, besides hiding the data structure responses, something that may also be useful.

A much more interesting alternative would create a script that just receives the json data from aws cli and with a map as argument, filters down the information to what I need. And I pretend do this by a simple shell redirect (pipe).

Right now, I can do something like that in Node.js script. Following illustrates exactly how I would like to operates in Python: 在此处输入图像描述

Unless someone says that does not have a way to redirect shell command output to a python script, I prefer not to maintain just this piece of code in a different language than all the others.

How do I do the shell output redirection to a python script?

You could read sys.stdin in Python. My understanding of the above javascript code is translates to this in Python:

import sys
import json

object_data = json.loads(sys.stdin.read())
print(object_data['description'])

However do note that the AWS API has a Python SDK, so you probably don't have to pipe around these JSON outputs at all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM