python 字符串替换 $
发布网友
发布时间:2022-04-23 13:09
我来回答
共1个回答
热心网友
时间:2022-04-03 05:46
试试看将替换后生成的字符串再生成模板后再次替换
tmpl = string.Template(...)
string.Template(tmpl.safe_substitute(**kwg)).safe_substitute(**kwg)
#!/usr/bin/python
# encoding: utf-8
#
# filename: stringTemplateLearning.py
# author: Tim Wang
# date: June, 2013
import string
tmpl = string.Template("""
$app:
home: $home
data: $data
""")
kwgs = dict(
app="App_Proj",
home="/home/tim/project/$app",
data="$home/data",
)
context = tmpl.safe_substitute(kwgs)
while "$" in context:
tmpl = string.Template(context)
context = tmpl.safe_substitute(kwgs)
print context
>python -u "stringTemplateLearning.py"
App_Proj:
home: /home/tim/project/App_Proj
data: /home/tim/project/App_Proj/data
>Exit code: 0 Time: 0.044