common 模块的 context.py

1.context.py  是写正则表达式的,源码如下:

import re
#s 是目标字符串
#dict 是替换的内容
#找到目标字符串里面的标识符KEY,去d里面拿到替换的值
#替换到s 里面去,然后仔返回

def replace(s,dict):
p = "\$\{(.*?)}"
while re.search(p,s) is not None:
m = re.search(p,s)
key = m.group(1)
value = dict[key]
s = re.sub(p,value,s,count=1)
return s

s = '{"mobilephone":"${admin_user}","pwd":"${admin_pwd}"}'
data = {"admin_user":"17799801359","admin_pwd":"123456"}

s = replace(s,data)
print(s)

 

posted @ 2019-11-19 22:46  weihanchen  阅读(179)  评论(0编辑  收藏  举报