躺在沙灘上的小豬

          快樂的每一天

          python and Template.

          也許說(shuō)法不正確,但是目前我能理解的就是替換,替換指定文本中的內(nèi)容。
          例如:

          <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可以輕松搞定(當(dāng)然更復(fù)雜的template需要更深入的去學(xué)習(xí)...)。
          --------------

          import re 
          import sys 
          
          class Template: 
              """ 
                  簡(jiǎn)單的模板,用于替換指定文本的特殊字符 
             例如:$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): 
                  """ 
                      返回所有的要求替換匹配的表達(dá)式 
                  """ 
                  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): 
                  """ 
                      讀取文件的內(nèi)容 
                  """ 
                  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':'測(cè)試','content':'的嘎公司今后;飛機(jī)庫(kù)'} 
              t = Template(filename,params) 
              print t.parse()

          ------------- ------------- ------------- ------------- ------------- ------------- ------------- -------------

          這樣,我們的python代碼可以更簡(jiǎn)單點(diǎn)了

          我們將以前的代碼做修改,如下:

          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

          關(guān)于template的進(jìn)一步思考:
          一:不用那么復(fù)雜的制定文件全名:
          'D:\\workspace\\style\\test
          template.xt'
          代碼實(shí)現(xiàn)從當(dāng)前的classpath中自動(dòng)尋找該文件,并且制定文件后綴名。
          那么就變的如下簡(jiǎn)單了。

          t = Template('template',params)

          posted on 2005-09-20 18:53 martin xus 閱讀(1541) 評(píng)論(1)  編輯  收藏 所屬分類: python

          主站蜘蛛池模板: 晋江市| 巧家县| 霍林郭勒市| 闽清县| 长乐市| 吴堡县| 江西省| 普洱| 金寨县| 屯昌县| 英吉沙县| 瓦房店市| 和平县| 开远市| 格尔木市| 平阳县| 寿宁县| 三明市| 呼和浩特市| 河北区| 加查县| 惠州市| 彰武县| 皋兰县| 内乡县| 山阴县| 东乌| 栾川县| 元氏县| 罗定市| 乌兰察布市| 长海县| 平邑县| 奇台县| 佛山市| 隆子县| 收藏| 铁岭市| 固镇县| 舞钢市| 靖西县|