解析初学Python时注意事项

那天用Python写代码感觉非常意外,今天用了一天的时间进行学习和研究,当然也是为我们的那个程序服务
首页 新闻资讯 行业资讯 解析初学Python时注意事项

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

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

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

复制

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)
  • 1.

  • 2.

  • 3.

  • 4.

  • 5.

  • 6.

  • 7.

  • 8.

  • 9.

  • 10.

  • 11.

  • 12.

  • 13.

  • 14.

  • 15.

  • 16.

  • 17.

  • 18.

  • 19.

  • 20.

  • 21.

  • 22.

  • 23.

  • 24.

  • 25.

  • 26.

  • 27.

  • 28.

  • 29.

  • 30.

  • 31.

  • 32.

  • 33.

  • 34.

  • 35.

  • 36.

  • 37.

  • 38.

  • 39.

  • 40.

  • 41.

  • 42.

  • 43.

  • 44.

  • 45.

  • 46.

  • 47.

  • 48.

  • 49.

  • 50.

  • 51.

  • 52.

  • 53.

  • 54.

  • 55.

  • 56.

  • 57.

  • 58.

  • 59.

  • 60.

  • 61.

  • 62.

  • 63.

  • 64.

  • 65.

  • 66.

  • 67.

  • 68.

  • 69.

  • 70.

  • 71.

  • 72.

  • 73.

  • 74.

  • 75.

  • 76.

  • 77.

  • 78.

  • 79.

  • 80.

  • 81.

  • 82.

  • 83.

  • 84.

  • 85.

  • 86.

  • 87.

  • 88.

  • 89.

  • 90.

  • 91.

  • 92.

  • 93.

  • 94.

  • 95.

  • 96.

  • 97.

  • 98.

  • 99.

  • 100.

  • 101.

  • 102.

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

【编辑推荐】

  1. 如何使Python嵌入C++应用程序?

  2. 深入探讨Ruby与Python语法比较

  3. Python学习资料介绍分享

  4. Python学习经验谈:版本、IDE选择及编码解决方案

  5. 浅析Python的GIL和线程安全

13    2010-02-03 10:21:46    初学Python