Skip to content

Commit a98e258

Browse files
authored
Create print_nested_object.py
1 parent 8a4a1fd commit a98e258

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

print_nested_object.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
def print_object(v, prefix=''):
2+
if isinstance(v, dict):
3+
for k, v2 in v.items():
4+
p2 = "{}['{}']".format(prefix, k)
5+
print_dict(v2, p2)
6+
elif isinstance(v, list):
7+
for i, v2 in enumerate(v):
8+
p2 = "{}[{}]".format(prefix, i)
9+
print_dict(v2, p2)
10+
else:
11+
print('{} = {}'.format(prefix, repr(v)))
12+
13+
14+
d = {'xml': {'config': {'portstatus': {'status': 'good'}, 'target': '1'}, 'port': '11'}, 'json': ['python', 'javascript']}
15+
16+
print_object(d)
17+
18+
19+
##output :
20+
['xml']['config']['portstatus']['status'] = good
21+
['xml']['config']['target'] = 1
22+
['xml']['port'] = 11
23+
['json'][0] = python
24+
['json'][1] = javascript

0 commit comments

Comments
 (0)