也许说法不正确,但是目前我能理解的就是替换,替换指定文本中的内容。
例如:
<a name="$code$"></a> <div class="infomark">$title$</div> <div class="info" style="background:#F4F4F4"> <pre> $content$ </pre> </div> <div align="right" class="right"><a href="#000">Top</a></div> <br> <br>
我要做的只是替换其中特定格式的 $code$,$title$,$conent$,而已。
那么我们用re可以轻松搞定(当然更复杂的template需要更深入的去学习...)。
--------------
import re import sys class Template: """ 简单的模板,用于替换指定文本的特殊字符 例如:$code$ """ def __init__(self,filename,params,prefix='$',suffix='$'): if not filename: print 'Error:' sys.exit(-1) else: self.filename=filename self.text='' self.params=params self.prefix=prefix self.suffix=suffix def getKeys(self): """ 返回所有的要求替换匹配的表达式 """ return params.keys() def parse(self): self.getContent() keys = self.params.keys() for key in keys: value = self.params[key] self.__replace(key,value) return self.text def getContent(self): """ 读取文件的内容 """ f = open(self.filename) lines = f.readlines() self.text = ''.join(line for line in lines) def __replace(self,key,value): #m = re.match(prefix+key+suffix,text) pattern = '.*\'+self.prefix+key+'\'+self.suffix+'.*' self.text = re.sub(pattern,value,self.text) if __name__=='__main__': filename = 'D:\workspace\style\test\template.xt' params = {'code':'001001','title':'测试','content':'的嘎公司今后;飞机库'} t = Template(filename,params) print t.parse()
------------- ------------- ------------- ------------- ------------- ------------- ------------- -------------
这样,我们的python代码可以更简单点了
我们将以前的代码做修改,如下:
import cx_Oracle from Template import * def parse(): '''generate the content html''' sql = '''select t.bz_code code, t.bz_title title, t.bz_content content from bz_czzs t order by t.bz_code''' connection = cx_Oracle.connect( 'etasadmin/etasadmin@zhongju' ) cursor = connection.cursor() cursor.execute(sql) item=cursor.fetchone() i=1; print 'begin' while item: i+=1 print 'parsing ',i,' item....' writeContent(item[0],item[1],str(item[2])) item=cursor.fetchone() def writeContent(code,title,content): filedir='D:\m\content\' params = {'code':code,'title':title,'content':content} t = Template('D:\workspace\style\test\template.xt',params) s = t.parse() out = open(filedir+code+".html",'w') out.write(s) out.flush() out.close() if __name__=='__main__': print 'parse..................' parse() print 'end'
参考文档:
http://docs.python.org/lib/module-re.html
关于template的进一步思考:
一:不用那么复杂的制定文件全名:
'D:\workspace\style\test
template.xt'
代码实现从当前的classpath中自动寻找该文件,并且制定文件后缀名。
那么就变的如下简单了。
t = Template('template',params)
相关推荐
Mako is a template library written in Python. It provides a familiar, non-XML syntax which compiles into Python modules for maximum performance. Mako's syntax and API borrows from the best ideas of ...
Deprecated Python modules, functions and methods asynchat asyncore dbm distutils grp importlib os re ssl tkinter venv Deprecated functions and types of the C API Deprecated Build Options ...
Design pattern is a well-known approach to solve some specific problems which each software ... Finally, the book also explains three behavioural patterns which are Command, Observer, and Template.
This course offers Python developers a detailed introduction to OpenCV 3, starting with installing and configuring your Mac, Windows, or Linux development environment along with Python 3. Learn about...
add_watermark_and_protect('filled_template.docx', 'watermarked_and_protected.docx') ``` #### 第三步:使用python-docx去除官方提示 完成上述步骤后,文档中将包含官方提示信息。此时,我们可以再次利用python...
if username == 'admin' and password == 'admin': return redirect(url_for('dashboard')) else: return render_template('login.html', error='Invalid username or password') else: return render_...
:hammer_and_wrench: 设置 确保已安装和 ,然后克隆此存储库。 克隆之后,导航到计算机上项目的根目录,并在终端应用程序(例如,Git Bash)中运行以下脚本: 安装Python依赖项: pip3 install -r requirements....
It also promotes the use of the Model-View-Template (MVT) architectural pattern, which separates concerns between data models, user interface rendering, and business logic. In conclusion, the design...
:hammer_and_wrench: 设置 确保已安装和 ,然后克隆此存储库。 克隆之后,导航到计算机上项目的根目录,并在终端应用程序(例如,Git Bash)中运行以下脚本: 安装Python依赖项: pip3 install -r requirements....
Python的`string.Template`模块或者`jinja2`模板引擎都可以用来处理这个任务。 4. **输出结果并保存到文件** 一旦我们有了模板和元数据,就可以生成代码字符串,然后将这些字符串写入到指定的文件中。例如,可以...
It provides a creator script to quickly create an project from template. Creating project Make sure you have installed Python 3 and requests library before proceeding. And then paste the following ...
It provides a creator script to quickly create an project from template. Creating project Make sure you have installed Python 3 and requests library before proceeding. And then paste the following ...
在Python编程领域,创建一个简单的登录界面是初学者和专业人士都经常遇到的任务。在这个场景中,我们将探讨如何使用Python和Flask框架来构建一个基本的Web登录界面。Flask是一个轻量级的Web服务器网关接口(WSGI)...
ffi.HANDLE(0) # template file handle (not used) ) ``` ### 示例应用 `pywincffi`常用于开发系统工具、后台服务、系统监控等场景,比如创建自启动项、管理系统进程、监控网络连接等。此外,它还可以用于创建低...
门挡(Browning and Adams,2014, ): pandoc: ://pandoc.org/installing.html graphviz: : pdflatex(可选,用于Beamer幻灯片输出) 安装 笔记: 默认情况下,门挡仅打印一个级别的链接,因此,如果跳过一...
awesome-python is a list of Python resources initiated and supported by vinta, including: web platforms, web scanners, web content extraction, template engines, databases, data visualization, image ...