1. What is Templatetemplate is a part of the string library in Python using template to change the data template without editing the application can also be modified by his subclass 2. The template for how the template works is a string that contains placeholders that map values to the template placeholder followed by a variable name to conform to the variable name rule in the Python syntax template ("$Name is friends with$Friend ") 3. Example
fromStringImportTemplate def main():Cart = [] Cart.append (Dict (item=' Coke ', price= One, qty=1)) Cart.append (Dict (item=' Cake ', price= A, qty=6)) Cart.append (Dict (item=' Fish ', Price =1, qty =4)) T = Template ("$qty * $item = $price") Total =0 Print "Cart" forDatainchCartPrintT.substitute (data) Total + = data["Price"]Print "Total:%s"% (total,)if__name__ = ="__main__": Main ()
4.template exception The interpreter throws a keyerror if the variable that is guided by the error placeholder does not match the dictionary. Because we don't have this key in our dictionary. Some placeholder-guided variables are not good. For example, this variable starts with a number. This causes the interpreter to throw a ValueError exception. 5. Safe_substitute () If we use this function, the template can handle these exceptions and return the string to us. If there is an exception to the placeholder variable, the placeholder in the returned string is unchanged and will not be substituted. For example, the Template ("$Name had$Money "). If money has errors here, then using Safe_substitute () The output is" James had$Money "6. You can use your favorite symbols to guide placeholder variables what we're going to do is overload the class property delimiter and modify the corresponding template strings and variables. Here we replace the default dollar $ sign with the C-address notation.
fromStringImportTemplate class MyTemplate(Template):delimiter =' & ' def main():Cart = [] Cart.append (Dict (item=' Coke ', price= One, qty=1)) Cart.append (Dict (item=' Cake ', price= A, qty=6)) Cart.append (Dict (item=' Fish ', Price =1, qty =4)) T = MyTemplate ("&qty * &item = &price") Total =0 Print "Cart" forDatainchCartPrintT.substitute (data) Total + = data["Price"]Print "Total:%s"% (total,)if__name__ = ="__main__": Main ()
The result of the operation is still the same.
Cart
1 * Coke = 11
6 * cake = 12
4 * Fish = 1
Total:24
7. Tips if you line output delimiter, you need to enter two consecutive delimiter. It can be thought that delimiter is an escape character. For example, in the case of delimiter is ' $ ':
>>> t = Template ("$You owe me $$0.")
>>> T.substitute (dict (you= ' James '))
"James owe me."
This is also true if you also want to change the naming convention for the variable names that follow the placeholder. After inheriting the template class, change the Class property Idpattern, whose default value is R "[_a-z][_a-z0-9]*". If you still have a need, want to change a part of a word. That's OK. Simply add the variable name after the placeholder with {}. Like what:
>>> t = Template ("The ${Back}yard is far away.")
>>> T.substitute (dict (back= ' ship '))
"The shipyard is far away."
[Note]python template templates