原创:多层嵌套字典无限遍历,实现当value值以特殊字符$开头,并且等于某项值时,用随机函数替换该参数

"""
处理前的字典
{'patient': {'avatarPic': '', 'facePic': '', 'busiInsurs': [], 'name': '$name', 'sex': '$sex', 'idNo': '$idNo',
'agebirthday': {'age': 7, 'isMonth': 0, 'birthday': '$birthday', 'isDay': 27, 'isShow': False}, 'age': 7, 'isMonth': '',
'isDay': 27, 'mobile': '$mobile', 'source': '', 'birthday': '$birthday', 'remark': '', 'idens': [{'type': '10', 'idNo': '$idNo'}],
'householdAddress': '', 'address': '', 'bloodType': '', 'marry': '', 'career': '', 'company': '', 'workAddress': '', 'country': '',
'nation': '', 'birthAddress': '', 'telephone': '', 'engName': '', 'objLabelList': [], 'anonymous': False, 'id': '', 'member': None,
'language': None, 'wechat': '', 'weibo': '', 'qq': '', 'email': '', 'iden': [{'type': '10', 'idNo': '$idNo'}], 'contactMobile': '',
'visitNos': [{'type': '', 'no': ''}], 'visitCards': []}, 'objLabelList': [], 'email': '', 'qq': '', 'wechat': '', 'weibo': ''}
"""
#递归遍历param
def get_dict_allkeys(dict_a,special_value,random_function):
if isinstance(dict_a,dict):
for key, value in dict_a.items():
if isinstance(value,str) and (value.startswith('$')) and value[1:len(value)] == special_value:
dict_a[key] = random_function
get_dict_allkeys(value,special_value,random_function)
elif isinstance(dict_a,list):
for k in dict_a:
if isinstance(k,dict):
for key,value in k.items():
if (value.startswith('$')) and value[1:len(value)] == special_value:
k[key] = random_function
get_dict_allkeys(value, special_value, random_function) #需要处理的参数都写到这里
def random_fun(data):
get_dict_allkeys(data, 'name', random_name())
get_dict_allkeys(data,'sex',random_sex())
get_dict_allkeys(data,'mobile',create_phone())
get_dict_allkeys(data,'birthday',creat_birthday())
get_dict_allkeys(data,'idNo',getRandomIdCard())
"""
处理后的字典
{'patient': {'avatarPic': '', 'facePic': '', 'busiInsurs': [], 'name': '李军', 'sex': '2', 'idNo': '13090220140108181X',
'agebirthday': {'age': 7, 'isMonth': 0, 'birthday': '1982-06-01', 'isDay': 27, 'isShow': False}, 'age': 7, 'isMonth': '',
'isDay': 27, 'mobile': '15130297893', 'source': '', 'birthday': '1982-06-01', 'remark': '',
'idens': [{'type': '10', 'idNo': '13090220140108181X'}], 'householdAddress': '', 'address': '', 'bloodType': '', 'marry': '',
'career': '', 'company': '', 'workAddress': '', 'country': '', 'nation': '', 'birthAddress': '', 'telephone': '', 'engName': '',
'objLabelList': [], 'anonymous': False, 'id': '', 'member': None, 'language': None, 'wechat': '', 'weibo': '', 'qq': '', 'email': '',
'iden': [{'type': '10', 'idNo': '13090220140108181X'}], 'contactMobile': '', 'visitNos': [{'type': '', 'no': ''}], 'visitCards': []},
'objLabelList': [], 'email': '', 'qq': '', 'wechat': '', 'weibo': ''}
""" 转载:该方法是获取嵌套的json key值集合。 若需要获取所有json嵌套的key,value值,修改对应return即可。
self.key_list = []
def get_dict_allkeys(self, dict_a):
"""
多维/嵌套字典数据无限遍历,获取json返回结果的所有key值集合
:param dict_a:
:return: key_list
"""
if isinstance(dict_a, dict): # 使用isinstance检测数据类型
for x in range(len(dict_a)):
temp_key = dict_a.keys()[x]
temp_value = dict_a[temp_key]
self.key_list.append(temp_key)
self.get_dict_allkeys(temp_value) # 自我调用实现无限遍历
elif isinstance(dict_a, list):
for k in dict_a:
if isinstance(k, dict):
for x in range(len(k)):
temp_key = k.keys()[x]
temp_value = k[temp_key]
self.key_list.append(temp_key)
self.get_dict_allkeys(temp_value)
return self.key_list

# 测试
dict01 = {"a": 1, "b": {"kk": {"nn": 111, "pp": "ppoii"}, "yy": "123aa", "uu": "777aa"},
"c": [{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6}]}
get_dict_allkeys(dict01) # 输出
['a', 'c', 'a', 'b', 'a', 'b', 'a', 'b', 'b', 'kk', 'pp', 'nn', 'yy', 'uu']

 

05-11 11:09