-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_success.py
81 lines (61 loc) · 1.3 KB
/
json_success.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/bin/python3
# JSON TUTORIAL
import json as js
mydict = {
"people": [
{
"name": "sam",
"age": 28,
"weight": 80
},
{
"name": "john",
"age": 22,
"weight": 20
},
{
"name": "kelly",
"age": 30,
"weight": 180
}
]
}
string = js.dumps(mydict, indent=4)
####################
## FILE FUNCTIONS ##
def _WRITE():
with open('test.json', 'w') as f:
f.write(string)
def _READ():
with open('test.json', 'r') as f:
data = js.loads(f.read())
return data
####################
data = _READ()
def data_by_subject(sub):
subject = []
for dictionary in data['people']:
subject.append(dictionary[sub])
return subject
def data_by_dictionary(d_num):
return data['people'][d_num]
def get_dictionary_from_value(value):
iter = 0
for i in data['people'][iter].values():
if value == i:
return data['people'][iter]
iter += 1
specific = get_dictionary_from_value('sam')
#specific = data_subject_in_dictionary(0, 'name')
names = []
age = []
weight = []
all_names = data_by_subject('name')
all_ages = data_by_subject('age')
all_weight = data_by_subject('weight')
dict1 = data_by_dictionary(0)
dict2 = data_by_dictionary(1)
dict3 = data_by_dictionary(2)
print(str(all_names), str(all_ages), str(all_weight))
print(str(dict1) + "\n" + str(dict2) + "\n" + str(dict3))
print(str(specific))