Python3.2 official document translation-template

Source: Internet
Author: User

8.2 Template

The String module contains a widely used class, which provides simple syntax support for end-user editing. This allows users to customize their applications without modifying the applications.

This format uses a placeholder name in the form of $ plus a valid python identifier (numbers, letters, and underscores. You can use braces on both sides of the placeholder to separate the strings with more letters and numbers. Use $ to create a single $ transcoding character.

>>> From string import Template

>>> T = Template ('$ {village} folk send $10 to $ cause .')

>>> T. substitute (village = 'notthen', cause = 'the ditch fund ')

'Nottinghamfolk send $10 to the ditch fund .'

If the placeholder is not provided in the dictionary or keyword parameter, the substitute () method throws a key error. User data may be incomplete in applications separated by mail merge. This method may be more suitable if safe_substitude () is used-if data is lost, it will not change the placeholder.

>>> T = Template ('Return the $ item to $ owner .')

>>> D = dict (item = 'unladen swallow ')

>>> T. substitute (d)

Traceback (most recent call last ):

...

KeyError: 'owner'

>>> T. safe_substitute (d)

'Return the unladen swallow to $ owner .'

A custom separator can be specified for a template derived class. For example, you can use the percent sign as a placeholder for the batch naming tool class of the image browser, such as the current date, image serial number, or file format.

Html webpage custom templates are called possible.

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.