解析初学Python时注意事项


初学Python时需要注意相关事项,首先初学Python时要先知道什么是Python?那么下面说一下什么是Python,所谓Python:是一种面向对象、直译式计算机程序设计语言,也是一种功能强大而完善的通用型语言。

我们目前要把一个表态HTML页面转换成PORTAL。由于表态页面数量很大,所以我们采用动态改写的方法。由于这篇的目的不是介绍我们的项目。所以直接说我的脚本。由于我们的工作,我们现在做操作前要对所以的静态页面进行简单的标记分析。这里主要分析TABLE,TR和TD。

下面贴下我的代码:初学Python主要是两个文件:

  1. import os, fnmatch  
  2.  
  3. # judge comment tag to delete comment statement  
  4. def judgeComment (line):  
  5.     openTag = line.find('<!--')  
  6.     closeTag = line.find('-->')  
  7.     if openTag != -1:  
  8.         if closeTag != -1:# <!--  --> 
  9.             return 1  
  10.         else:#<!--  
  11.             return 2  
  12.     elif closeTag != -1:#--> 
  13.         return 3  
  14.     else:#  
  15.         return 4  
  16.  
  17. # sort for a 2 dimension list(array)  
  18. def sortFor2di (listtosort):  
  19.     size = len(listtosort)  
  20.     for i in range(size-1):  
  21.         for j in range(i + 1,size):  
  22.             list1 = listtosort[i]  
  23.             list2 = listtosort[j]  
  24.             if list1[0] > list2[0]:  
  25.                 listtosort[i],listtosort[j] = listtosort[j],listtosort[i]  
  26.  
  27. # get all tags in a line in the form of list  
  28. def getLineTagList (line):  
  29.     taglist = []  
  30.     addTag2List (line,'table',taglist)  
  31.     addTag2List (line,'tr',taglist)  
  32.     addTag2List (line,'td',taglist)  
  33.     sortFor2di (taglist)  
  34.     return taglist  
  35.  
  36. def addTag2List (line,tag,taglist):  
  37.     pos = line.find('<'+tag)  
  38.     if pos != -1:  
  39.         taglist.append([pos,'<'+ tag + '>'])  
  40.     pos = line.find('</'+tag+'>')  
  41.     if pos != -1:  
  42.         taglist.append([pos,'</' + tag + '>'])  
  43.  
  44. def addDelTag(itemlist,stackList):  
  45.     tag = itemlist[1]  
  46.     res = 0 
  47.     res += judgeWhichTag (tag,'table',stackList)  
  48.     res += judgeWhichTag (tag,'tr',stackList)  
  49.     res += judgeWhichTag (tag,'td',stackList)  
  50.     if res != 0:  
  51.         return -1  
  52.     else:  
  53.         return 1  
  54.  
  55. #       
  56. def judgeWhichTag (tag,lable,stackList):  
  57.     if tag == '<' + lable + '>':  
  58.         stackList.append(lable)  
  59.         return 0  
  60.     elif tag == '</' + lable + '>':  
  61.         size = len(stackList)  
  62.         if size < 1: 
  63.             return -1  
  64.         elif stackList[size - 1] == lable:  
  65.             del(stackList[size -1 ])  
  66.             return 0  
  67.         else:  
  68.             return -1  
  69.     else:  
  70.         return 0  
  71.  
  72. # used to deal tag         
  73. def tagDeal (tag, line,stackList):  
  74.     openTag = line.find('<'+tag)  
  75.     closeTag = line.find('</'+tag+'>')  
  76.     if openTag != -1:  
  77.         stackList.append (tag)  
  78.         if closeTag == -1:  
  79.             return 1  
  80.     if closeTag != -1:  
  81.         size = len(stackList)  
  82.         if size < 1: 
  83.             return -1  
  84.         else:  
  85.             lastItem = stackList[size - 1]  
  86.             if lastItem != tag:  
  87.                 return -1  
  88.             else:  
  89.                 del (stackList[size - 1])  
  90.                 return 1  
  91.  
  92. def find (pattern,startdir=os.curdir):  
  93.     files = []  
  94.     os.path.walk(startdir,visitor,(pattern,files))  
  95.     files.sort()  
  96.     return files  
  97.  
  98. def visitor ((pattern,files),thisdir,names):  
  99.     for name in names:  
  100.         if fnmatch.fnmatch(name,pattern):  
  101.             fullpath = os.path.join(thisdir,name)  
  102.             files.append(fullpath) 

申明一下,我是初学Python。上面的程序写得很乱,以后有时间再修改或加点注释。当然很欢迎各位朋友给点意见。不过,最后的结果是我们的总共1000表态页面中共有200个页面这三种标签有错误。这就意味着有一大堆事情要处理。至于怎么做我们还没做好决定。

  1. 如何使Python嵌入C++应用程序?
  2. 深入探讨Ruby与Python语法比较
  3. Python学习资料介绍分享
  4. Python学习经验谈:版本、IDE选择及编码解决方案
  5. 浅析Python的GIL和线程安全

相关内容

    暂无相关文章

评论关闭