Last active
July 21, 2016 05:35
-
-
Save ishu3101/b8ad33e01650700eb963 to your computer and use it in GitHub Desktop.
How to read from stdin or from a file if no data is piped in Python.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# echo '{"_payload":{"navigationResult":{"list":[{"order_id":"2","date":"date"}]}}}' | python parser.py | |
# python parser.py --input input.json | |
# see: http://stackoverflow.com/questions/2264991/how-to-read-from-stdin-or-from-a-file-if-no-data-is-piped-in-python | |
import argparse | |
import json | |
def parse(json_data): | |
data = json.load(json_data) | |
resultlist = data['_payload']['navigationResult']['list'] | |
for order in resultlist: | |
print "[%s] - [%s]" %(order['order_id'],order['date']) | |
json_data.close() | |
def init(): | |
parser = argparse.ArgumentParser('Combined orders json parser') | |
parser.add_argument('--input', type = argparse.FileType('r'), default = '-') | |
args = parser.parse_args() | |
parse(args.input) | |
if __name__ == '__main__': | |
init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment