Template is undoubtedly a good thing, it can fix the format of string and reuse it. At the same time, Template also allows developers to consider the format and content of the string separately, which virtually reduces the pressure on developers.
Template belongs to a class in string, so if you want to use it, you can call it in the following way
from string import Template
Template has a special identifier $, which has the following rules:
Its main implementation method is $xxx, where xxx is a string that meets python naming rules, that is, it cannot start with a number, cannot be a keyword, etc.
If $xxx needs to be in contact with other strings, you can use { } Wrap xxx (it seems to use '()' before, one of my reference books says this, but the current version should only use '{}'). For example, aaa${xxx}aaa
There are two important methods in Template: substitute and safe_substitute.
Both methods can return strings by getting parameters
>>s=Template(There $a and $b) >>print s.subtitute(a='apple',b='banana') There apple and banana >>print s.safe_substitute(a='apple',b='banbana') There apple and banbana
You can also pass data directly by getting the dictionary, like this
>>s=Template(There $a and $b) >>d={'a':'apple','b':'banbana'} >>print s.substitute(d) There apple and banbana
The difference between them lies in the way they handle missing parameters.
The implementation of Template is to first initialize a string through Template. These strings contain keys one by one. By calling substitute or safe_subsititute, the key value is matched with the parameters passed in the method, thereby importing the string at the specified location. One advantage of this method is that there is no need to print '%s' or the like. The order of each parameter must be fixed. As long as the key is correct, the value can be inserted correctly. In this way, you can breathe a sigh of relief when inserting a lot of data. But even if there is such a lazy method, there is still no guarantee that there will be no errors. What if one less key is entered?
Substitute is a serious method. If there is a key that is not entered, an error will definitely be reported. Although it will be ugly, the problem can be found.
safe_substitute will not report an error, but input $xxx directly into the result string, such as
there apple and $b
. The advantage is that the program is always correct, and you don’t have to be confused by errors one by one. Worried.
Template can be inherited, and its subclasses can perform some 'personalization' operations...
The $ character can be changed to other characters, such as "#", by modifying the delimiter field. However, new identifiers need to comply with regular expression specifications.
You can modify the key naming rules by modifying the idpattern. For example, it is stipulated that the first character must start with a, which is very good for standardizing naming. Of course, this is also achieved through regular representation.
from string import Template class MyTemplate(Template): delimiter = "#" idpattern = "[a][_a-z0-9]*" def test(): s='#aa is not #ab' t=MyTemplate(s) d={'aa':'apple','ab':'banbana'} print t.substitute(d) if name=='main': test()
The above is the detailed content of Introduction to the use of Template in python. For more information, please follow other related articles on the PHP Chinese website!