需求:将python字典中所有的键中的.换成。
难点:字典可能包含字典,也可能包含列表
解决思路:
python改键很复杂, 这里先将字典转换成字符串,将所有.换成。之后再把所有值中的。改成.
递归转换python字典值中的代码:
def update_dict(root):
if isinstance(root, str):
root = root.replace('。', '.')
else:
for root_key, root_value in root.items():
if isinstance(root[root_key], dict):
result = update_dict(root[root_key])
root[root_key] = result
elif isinstance(root[root_key], list):
li = []
for i in root[root_key]:
result = update_dict(i)
li.append(result)
root[root_key] = li
else:
if isinstance(root[root_key], str):
root[root_key] = root[root_key].replace('。','.')
return root