Python字符串模板

系统教程 行业动态 更新时间:2024-06-14 17:02:17
Python字符串模板 - 使用百分比分隔符(Python String Template - Using percentage delimiter)

我正在尝试学习字符串模板模块并使用替代分隔符面临打嗝。

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变量名以%为前缀,但不起作用。

>>> 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} $a3

temp_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 '

更多推荐

本文发布于:2023-04-21 18:34:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/77a0f497b00969179a4d5313f616082c.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   模板   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!