解决方案:
def replace_new(s):
# 读取出来的数据用context的数据去替换,loan_id 动态替换
p = '\$\{(.*?)}'
while re.search(p, s):
m = re.search(p, s)
key = m.group(1)
if hasattr(Context, key):
value = str(getattr(Context, key)) #重点: 取出来的是int类型,所以应该转换成字符串去替换
s = re.sub(p, value, s, count=1)
else:
return None
return s
s = '{"mobilephone": "${borrow_user}", "pwd": "${borrow_pwd}"}'
s = replace_new(s)
print(s)