我正在尝试学习字符串模板模块,并面临使用替代分隔符时出现的问题。在
temp_text_dollar
具有前缀为$
的变量名,并且工作正常
>>> import string
>>> val = {'a1':'VAL1' , 'a2' : 'VAL2' , 'a3' : 'VAL3' , 'a4' : 'VAL4' }
>>> temp_text_dollar = string.Template(" This is a sample text ${a1} $a3 ")
>>> print temp_text_dollar.substitute(val)
This is a sample text VAL1 VAL3
>>> print temp_text_dollar.delimiter
$
>>> print temp_text_dollar.idpattern
[_a-z][_a-z0-9]*
>>> print temp_text_dollar.template
This is a sample text ${a1} $a3
temp_text_pct
具有前缀为%
的变量名,它不起作用。在
看起来像是打字错误,我无法破解。在
string.Template
可以用来替换%
变量吗?在
您从模板创建了一个模板:
temp_text_amp
不是字符串。这就是你看到的回溯的原因。在改为从字符串创建模板对象:
^{pr2}$下一个问题是您将
idpattern
限制为字母:但实际的模板字符串也使用数字。在
这很好用:
相关问题 更多 >
编程相关推荐