解析初學(xué)Python時(shí)注意事項(xiàng)
初學(xué)Python時(shí)需要注意相關(guān)事項(xiàng),首先初學(xué)Python時(shí)要先知道什么是Python?那么下面說(shuō)一下什么是Python,所謂Python:是一種面向?qū)ο?、直譯式計(jì)算機(jī)程序設(shè)計(jì)語(yǔ)言,也是一種功能強(qiáng)大而完善的通用型語(yǔ)言。
我們目前要把一個(gè)表態(tài)HTML頁(yè)面轉(zhuǎn)換成PORTAL。由于表態(tài)頁(yè)面數(shù)量很大,所以我們采用動(dòng)態(tài)改寫(xiě)的方法。由于這篇的目的不是介紹我們的項(xiàng)目。所以直接說(shuō)我的腳本。由于我們的工作,我們現(xiàn)在做操作前要對(duì)所以的靜態(tài)頁(yè)面進(jìn)行簡(jiǎn)單的標(biāo)記分析。這里主要分析TABLE,TR和TD。
下面貼下我的代碼:初學(xué)Python主要是兩個(gè)文件:
- import os, fnmatch
 - # judge comment tag to delete comment statement
 - def judgeComment (line):
 - openTag = line.find('<!--')
 - closeTag = line.find('-->')
 - if openTag != -1:
 - if closeTag != -1:# <!-- -->
 - return 1
 - else:#<!--
 - return 2
 - elif closeTag != -1:#-->
 - return 3
 - else:#
 - return 4
 - # sort for a 2 dimension list(array)
 - def sortFor2di (listtosort):
 - size = len(listtosort)
 - for i in range(size-1):
 - for j in range(i + 1,size):
 - list1 = listtosort[i]
 - list2 = listtosort[j]
 - if list1[0] > list2[0]:
 - listtosort[i],listtosort[j] = listtosort[j],listtosort[i]
 - # get all tags in a line in the form of list
 - def getLineTagList (line):
 - taglist = []
 - addTag2List (line,'table',taglist)
 - addTag2List (line,'tr',taglist)
 - addTag2List (line,'td',taglist)
 - sortFor2di (taglist)
 - return taglist
 - def addTag2List (line,tag,taglist):
 - pos = line.find('<'+tag)
 - if pos != -1:
 - taglist.append([pos,'<'+ tag + '>'])
 - pos = line.find('</'+tag+'>')
 - if pos != -1:
 - taglist.append([pos,'</' + tag + '>'])
 - def addDelTag(itemlist,stackList):
 - tag = itemlist[1]
 - res = 0
 - res += judgeWhichTag (tag,'table',stackList)
 - res += judgeWhichTag (tag,'tr',stackList)
 - res += judgeWhichTag (tag,'td',stackList)
 - if res != 0:
 - return -1
 - else:
 - return 1
 - #
 - def judgeWhichTag (tag,lable,stackList):
 - if tag == '<' + lable + '>':
 - stackList.append(lable)
 - return 0
 - elif tag == '</' + lable + '>':
 - size = len(stackList)
 - if size < 1:
 - return -1
 - elif stackList[size - 1] == lable:
 - del(stackList[size -1 ])
 - return 0
 - else:
 - return -1
 - else:
 - return 0
 - # used to deal tag
 - def tagDeal (tag, line,stackList):
 - openTag = line.find('<'+tag)
 - closeTag = line.find('</'+tag+'>')
 - if openTag != -1:
 - stackList.append (tag)
 - if closeTag == -1:
 - return 1
 - if closeTag != -1:
 - size = len(stackList)
 - if size < 1:
 - return -1
 - else:
 - lastItem = stackList[size - 1]
 - if lastItem != tag:
 - return -1
 - else:
 - del (stackList[size - 1])
 - return 1
 - def find (pattern,startdir=os.curdir):
 - files = []
 - os.path.walk(startdir,visitor,(pattern,files))
 - files.sort()
 - return files
 - def visitor ((pattern,files),thisdir,names):
 - for name in names:
 - if fnmatch.fnmatch(name,pattern):
 - fullpath = os.path.join(thisdir,name)
 - files.append(fullpath)
 
申明一下,我是初學(xué)Python。上面的程序?qū)懙煤軄y,以后有時(shí)間再修改或加點(diǎn)注釋。當(dāng)然很歡迎各位朋友給點(diǎn)意見(jiàn)。不過(guò),***的結(jié)果是我們的總共1000表態(tài)頁(yè)面中共有200個(gè)頁(yè)面這三種標(biāo)簽有錯(cuò)誤。這就意味著有一大堆事情要處理。至于怎么做我們還沒(méi)做好決定。
【編輯推薦】















 
 
 
 
 
 
 