我正在尝试学习字符串模板模块并使用替代分隔符面临打嗝。
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} $a3temp_text_pct变量名以%为前缀,但不起作用。
>>> temp_text_pct = string.Template(" This is a sample text %a1 %a3 ") >>> class MyTemplate(string.Template): ... delimiter = '%' ... idpattern = '[a-z]*' ... >>> t2 = MyTemplate(temp_text_pct) >>> print t2.substitute(val) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/string.py", line 172, in substitute return self.pattern.sub(convert, self.template) TypeError: expected string or buffer >>> print t2.delimiter % >>> print t2.idpattern [a-z]*看起来像拼写错误,我无法破解它。
可以用string.Template替换%变量吗?
I am trying to learn string Template module and facing hiccups using alternative delimiter.
temp_text_dollar has variable names prefixed by $ and it works fine
>>> 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} $a3temp_text_pct has variable names prefixed by % and it doesn't work.
>>> temp_text_pct = string.Template(" This is a sample text %a1 %a3 ") >>> class MyTemplate(string.Template): ... delimiter = '%' ... idpattern = '[a-z]*' ... >>> t2 = MyTemplate(temp_text_pct) >>> print t2.substitute(val) Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib/python2.7/string.py", line 172, in substitute return self.pattern.sub(convert, self.template) TypeError: expected string or buffer >>> print t2.delimiter % >>> print t2.idpattern [a-z]*Looks like typo error and I am unable to crack it.
Can string.Template be used to replace % variables ?
最满意答案
您是从模板创建的模板 :
>>> temp_text_amp = string.Template(" This is a sample text %a1 %a3 ") [...] >>> t2 = MyTemplate(temp_text_amp)temp_text_amp 不是字符串 。 这就是导致你看到的追溯的原因。
从字符串创建模板对象:
t2 = MyTemplate(" This is a sample text %a1 %a3 ")您的下一个问题是您将idpattern限制为仅字母 :
idpattern = '[a-z]*'但是你的实际模板字符串也使用数字。
这很好用:
>>> import string >>> class MyTemplate(string.Template): ... delimiter = '%' ... idpattern = '[a-z0-9]*' ... >>> t2 = MyTemplate(" This is a sample text %a1 %a3 ") >>> val = {'a1':'VAL1' , 'a2' : 'VAL2' , 'a3' : 'VAL3' , 'a4' : 'VAL4' } >>> t2.substitute(val) ' This is a sample text VAL1 VAL3 'You created a template from a template:
>>> temp_text_amp = string.Template(" This is a sample text %a1 %a3 ") [...] >>> t2 = MyTemplate(temp_text_amp)temp_text_amp is not a string. This is what is causing the traceback you see.
Create your template object from a string instead:
t2 = MyTemplate(" This is a sample text %a1 %a3 ")Your next problem is that you limited your idpattern to just letters:
idpattern = '[a-z]*'but your actual template string uses digits too.
This works just fine:
>>> import string >>> class MyTemplate(string.Template): ... delimiter = '%' ... idpattern = '[a-z0-9]*' ... >>> t2 = MyTemplate(" This is a sample text %a1 %a3 ") >>> val = {'a1':'VAL1' , 'a2' : 'VAL2' , 'a3' : 'VAL3' , 'a4' : 'VAL4' } >>> t2.substitute(val) ' This is a sample text VAL1 VAL3 '更多推荐
发布评论