Need to pass argument while accessing python json key -
i need access json file using command line argument
my code python.py:
######################################### import json import sys sys import argv pprint import pprint open('cdh/cdh5.json') data_file: data = json.load(data_file) pprint(data['projects']['abcd']['track-branch']) #################################################
so , instead of project abcd , need pass abcd command line while executing python script.
eg1: python.py abcd
will : pprint(data['projects']['abcd']['track-branch'])
eg2: python.py efgh
will : pprint(data['projects']['efgh']['track-branch'])
i tried doing this:-
pprint(data['projects']['sys.args']['track-branch'])
gives me below error :
pprint(data['projects']['sys.argv']['track-branch']) keyerror: 'sys.argv'
to access arguments pass in command line, need use sys.argv
, have got correct.
but should use is, not pass string , doing.
also, sys.argv
list of arguments command line, first element of sys.argv
file executed (like python.py
). need use second element. example -
pprint(data['projects'][sys.argv[1]]['track-branch'])
Comments
Post a Comment