-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask2.py
More file actions
60 lines (58 loc) · 1.65 KB
/
task2.py
File metadata and controls
60 lines (58 loc) · 1.65 KB
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
"""
Lab 2 Task 2
"""
import json
def if_dict(data):
"""
if the json file begins with dict
"""
print(list(data.keys()))
while True:
inp_k = input("Enter a key:")
if not inp_k:
break
if type(data) == list:
inp_k = int(inp_k)
data = data[inp_k] #заходим 'глибше'
if type(data) == list:
print(f'Number of elements: {len(data)}')
elif type(data) == dict:
print(f'Dict keys are: {list(data.keys())}')
else:
print(f'Value is: {data}')
break
return
def if_list(data):
"""
if the json file begins with list
"""
i = int(input('Enter an index of dict:'))
print(list(data[i].keys()))
while True:
inp_k = input("Enter a key:")
if not inp_k:
break
if type(data[i]) == list:
inp_k = int(inp_k)
data[i] = data[i][inp_k]
if type(data[i]) == list:
print(f'Number of elements: {len(data[i])}')
elif type(data[i]) == dict:
print(f'Dict keys are: {list(data[i].keys())}')
else:
print(f'Value is: {data[i]}')
break
return
if __name__ == "__main__":
path = input("Enter the name of file:")
try:
with open(path,'r') as file:
data = json.load(file)
if type(data) == list:
if_list(data)
elif type(data) == dict:
if_dict(data)
else:
print('Wrong file type')
except FileNotFoundError:
print("No such file")