-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParsing JSON
48 lines (42 loc) · 1.46 KB
/
Parsing JSON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
from __future__ import print_function
import json
import sys
from prettytable import PrettyTable
x=PrettyTable()
## Reading file as command argument
with open(sys.argv[1]) as data_file:
data = json.load(data_file)
options = dict()
for index, value in enumerate(sorted(data['nodes'][0].keys()), 1):
options[index] = value
print("{0}: {1}".format(index,value))
def display(inputlistarguments):
'''
Print the output as per user input selection.
'''
x.field_names=inputlistarguments
for i in range(0, len(data['nodes'])):
x.add_row([data['nodes'][i][eachargument] \
if type(data['nodes'][i][eachargument]) != list else data['nodes'][i][eachargument][0] \
for eachargument in inputlistarguments])
print(x.get_string())
def show(inputlist):
'''
Map the input selection to dictionary keys.
'''
parameter=[]
for eachvalue in inputlist:
parameter.append(options[eachvalue])
display(parameter)
def input_validation():
'''
Get the input from user in comma separated format. Using map to conver the
map to integer. Check whether entered input is from dictionary keys.
'''
inputvalues=str(input("Enter the comma separated numeric values. "))
inputvalues = list(map(int, inputvalues.split(',')))
if set(inputvalues).issubset(set(options.keys())):
show(inputvalues)
else:
raise('One or more imput values are invalid')
input_validation()