美文网首页
Python:string中的Template使用

Python:string中的Template使用

作者: 紫荆逸 | 来源:发表于2025-08-13 18:28 被阅读0次

PyCharm中写的.py代码:

可用于接口关联参数动态传递

使用方式:


#--------Template 使用---------
"""
如果碰到特殊符号${}
它会自动用字典的 key 去替换掉${}同名的变量
"""

from string import Template


#例一
penson = {"age":"12"}
desc = "小张今年${age}岁了"
print(Template(desc).substitute(penson))

#例二:可用于接口关联
aa = {"token":"124235cefdesdtvertvwe"}
url = "http://www.baidu.com?token=${token}"
print(Template(url).substitute(aa))

相关文章

网友评论

      本文标题:Python:string中的Template使用

      本文链接:https://www.haomeiwen.com/subject/timiojtx.html