【发布时间】:2016-11-18 11:10:06
【问题描述】:
test_dict = {'path': '/home/user/user_name','device1': {'IP': '10.10.10.10','password': 'pwd1','username': 'user1','name':'USER_DEFINED'},'device2': {'IP': '11.11.11.11','password': 'pwd2','username': 'user2','name':'USER_DEFINED_TEST'}}
keys_to_be_updated = "path,device1/name,device2/name"
values_to_be_updated = "/home/user/user_name/ABCD,USER_TESTER1,USER_TESTER2"
k = 0
list_of_values = values_to_be_updated.split(",")
if keys_to_be_updated != "":
for i in keys_to_be_updated.split(","):
for j in i.split("/"):
tmp = "['"+str(j)+"']"
# test_dict[tmp] = list_of_values[k]
# How to assign values to multi level dictionary here
# test_dict['path'] = list_of_values[k]
# test_dict['device1'][name'] = list_of_values[k]
# test_dict['device2'][name'] = list_of_values[k]
k = k+1
如何更新上述代码中的多级字典?最后,我应该得到更新了新值的字典。
【问题讨论】:
标签: python dictionary nested