I'm making regex based search engine.
For example if a query matches (?P<filename>\\d+@\\d+)
then I want to run the query {"table": "records", "filter": {"filename": "*filename"}}
. But before doing so I want to replace "*filename" with say "2786@20150510201045".
So basically I want to loop through a nested dictionary and replace anything starting with * with the value from the corresponding regex named group. The closest I've found is Loop through all nested dictionary values?
def myprint(d, groups):
new_d = {}
for k, v in d.items():
if isinstance(v, dict):
new_d[k] = myprint(v, groups)
elif isinstance(v, str):
new_d[k] = groups[v[1:]] if v.startswith('*') else v
else:
new_d[k] = v
return new_d
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.