日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python 论坛模板_python模板 - PH的个人空间 - OSCHINA - 中文开源技术交流社区

發(fā)布時(shí)間:2024/1/23 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 论坛模板_python模板 - PH的个人空间 - OSCHINA - 中文开源技术交流社区 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

看Python幫助文檔模板部分,抄下來以備記憶

11.2. Templating

The string module includes a versatile Template class with a simplified syntax suitable for editing by end-users. This allows users to customize their applications without having to alter the application.

The format uses placeholder names formed by $ with valid Python identifiers (alphanumeric characters and underscores). Surrounding the placeholder with braces allows it to be followed by more alphanumeric letters with no intervening spaces. Writing $$ creates a single escaped $:

>>>from string import Template >>>t = Template('${village}folk send $$10 to $cause.') >>>t.substitute(village='Nottingham', cause='the ditch fund') 'Nottinghamfolk send $10 to the ditch fund.'

The substitute() method raises a KeyError when a placeholder is not supplied in a dictionary or a keyword argument. For mail-merge style applications, user supplied data may be incomplete and the safe_substitute() method may be more appropriate — it will leave placeholders unchanged if data is missing:

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

Template subclasses can specify a custom delimiter. For example, a batch renaming utility for a photo browser may elect to use percent signs for placeholders such as the current date, image sequence number, or file format:

>>>import time, os.path >>>photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg'] >>>class BatchRename(Template): ... delimiter = '%' >>>fmt = raw_input('Enter rename style (%d-date %n-seqnum%f-format): ') Enter rename style (%d-date %n-seqnum %f-format): Ashley_%n%f >>>t = BatchRename(fmt) >>>date = time.strftime('%d%b%y') >>>for i, filename in enumerate(photofiles): ... base, ext = os.path.splitext(filename) ... newname = t.substitute(d=date, n=i, f=ext) ... print '{0} --> {1}'.format(filename, newname) img_1074.jpg --> Ashley_0.jpg img_1076.jpg --> Ashley_1.jpg img_1077.jpg --> Ashley_2.jpg

Another application for templating is separating program logic from the details of multiple output formats. This makes it possible to substitute custom templates for XML files, plain text reports, and HTML web reports.

總結(jié)

以上是生活随笔為你收集整理的python 论坛模板_python模板 - PH的个人空间 - OSCHINA - 中文开源技术交流社区的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。