Issue
dictionary={' items': { 'heading': 'Maps','description':'Maps123','imagepath':/music/images/','config':{'config1':12,'config2':123}},'db':{'username':'xyz','password':'xyz'},'version':'v1'}
I want the output in the format:
items/heading: Maps
items/description: Maps123
items/image_path: /music/images/v2/web_api-music.png
items/config/config1: abcd
items/config/config2: hello
db/username: xyz
db/password: xyz
version: v1
Solution
The answer of script0 results in an output that starts with a forward slash (/
). I modified script0's code a little:
def flatten(x, parent=''):
for key in x.keys():
if isinstance(x[key], dict) and parent == '': flatten(x[key], key)
elif isinstance(x[key], dict) and parent != '': flatten(x[key], f"{parent}/{key}")
elif not isinstance(x[key], dict) and parent == '': print(f"{key}: {x[key]}")
else: print(f"{parent}/{key}: {x[key]}")
dictionary= {'items': { 'heading': 'Maps','description':'Maps123','imagepath':'/music/images/','config':{'config1':12,'config2':123}},'db':{'username':'xyz','password':'xyz'},'version':'v1'}
flatten(dictionary)
Output (without the leading /):
items/heading: Maps
items/description: Maps123
items/imagepath: /music/images/
items/config/config1: 12
items/config/config2: 123
db/username: xyz
db/password: xyz
version: v1
Please note that you could also create a new dictionary with the left-hand side of the output as the keys and the right-hand side as the values.
For example:
new_dict = {}
def flatten(x, parent=''):
for key in x.keys():
if isinstance(x[key], dict) and parent == '': flatten(x[key], key)
elif isinstance(x[key], dict) and parent != '': flatten(x[key], f"{parent}/{key}")
elif not isinstance(x[key], dict) and parent == '': new_dict[key] = x[key]
else: new_dict[f"{parent}/{key}"] = x[key]
dictionary= {'items': { 'heading': 'Maps','description':'Maps123','imagepath':'/music/images/','config':{'config1':12,'config2':123}},'db':{'username':'xyz','password':'xyz'},'version':'v1'}
flatten(dictionary)
print(new_dict)
Output:
{'items/heading': 'Maps', 'items/description': 'Maps123', 'items/imagepath': '/music/images/', 'items/config/config1': 12, 'items/config/config2': 123, 'db/username': 'xyz', 'db/password': 'xyz', 'version': 'v1'}
Answered By - Bjorn B.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.