Python Template User Guide

Source: Internet
Author: User
This article mainly explains how to use and use templates in python. it is very practical. if you have any need, you can refer to the Template. it is undoubtedly a good thing to fix the string format, reuse. At the same time, the Template allows developers to consider the string format and content separately, which virtually reduces the pressure on developers.

Template is a class in string, so you can use the following method to call

from string import Template

A Template has a special identifier $, which has the following rules:

Its main implementation method is $ xxx, where xxx is a string that meets the python naming rules, that is, it cannot start with a number or be a keyword.

If $ xxx needs to be in contact with other strings, use {} to package xxx (previously '()', which is written in one of my reference books, but the current version should only use '{}'). For example, aaa $ {xxx} aaa

Template has two important methods: substitute and safe_substitute.

Both methods can return strings by obtaining 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 directly transmit data by obtaining the dictionary, such

>>s=Template(There $a and $b)>>d={'a':'apple','b':'banbana'}>>print s.substitute(d)There apple and banbana

The difference between them is the processing method when parameters are missing.

The implementation of Template is to first initialize a string through Template. These strings contain keys. Call substitute or safe_subsititute to correspond the key value to the parameter passed in the method, so as to import the string at the specified position. One advantage of this method is that you do not 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 correctly inserted. In this way, you can breathe a sigh of relief when inserting a lot of data. However, even if there is such a method of laziness, there is still no guarantee that no error will be made. what if one key is input less?

Substitute is a serious method. if a key is not input, an error is returned. Although difficult to see, you can find problems.

Safe_substitute does not report an error, but directly enters $ xxx into the result string, as shown in

there apple and $b

The advantage of this is that the program is always right and there is no need to be overwhelmed by errors.

A Template can be inherited, and its subclass can perform some 'personalization 'operations...

By modifying the delimiter field, you can change the $ character to another character, such as "#". However, the new identifier must comply with the regular expression specifications.

By modifying idpattern, you can modify the key naming rules. for example, it is recommended that the first character must start with a, which is good for standard naming. Of course, this is also implemented through regular expression.

from string import Templateclass 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()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.