We are using string template to replace some values in Python String by supplying a dict . It works fine . However despite using safe_substitute this below code doesnt work as expected . Any idea why ?
from string import Template
obj = Template("$$Tag$$")
obj.safe_substitute({})
Output : '$Tag$' Expected : '$$Tag$$'
(As there is no value to replace in the dict supplied .)
This same issue happens for '####' if I make '##' as the delimiter . Anyone knows why is it ignoring an extra delimiter ? Just trying to understand what happens under the hood .
From docs :
$$
is an escape; it is replaced with a single$
.
If you want double $
, then you can use something like:
>>> obj = Template("$$$$Tag$$$$")
>>> obj.safe_substitute({})
'$$Tag$$'
>>> obj.safe_substitute({'Tag':1})
'$$Tag$$'
>>> obj = Template("$$$Tag$$$") #First $ escapes the second $
>>> obj.safe_substitute({'Tag':1})
'$1$$'
From the doc :
"$$" is an escape; it is replaced with a single "$".
You have to put two dollar signs for each one that you want in the output:
>>> Template('$$$$Tag$$$$').substitute()
'$$Tag$$'
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.