Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.88 KB | None | 0 0
  1. # recursive search and replace
  2. # pass an empty string intialally with item_map
  3. # pass epty dict into result_dict
  4. def recurse(data, pat, item_map, result_dict):
  5.     if isinstance(data, dict):
  6.         for key in data:
  7.             if data[key] == pat:
  8.                 # when you get down prep it
  9.                 cur_path = item_path + '.'+key
  10.                 for task_obj in enumerate(data):
  11.                     key_name = task_obj[1]
  12.                 if data[key] == pat:
  13.                     data[key] = input("Input a dict value: ")
  14.                     # set in here so you can use it later in program with "fully qualified string key"
  15.                     result_dict[cur_path] = data[key]
  16.             else:
  17.                 # prep for going deeper
  18.                 item_map += '.'+key
  19.                 # and pass through
  20.                 recurse(data[key], pat, item_map, result_dict)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement