Notes for beginners of Python

Source: Internet
Author: User

When learning Python, you need to pay attention to related matters. First, You Need To Know What Python is when learning Python? So what is Python, the so-called Python: it is an object-oriented, literal-translation computer programming language, and also a powerful and complete general-purpose language.

At present, we need to convert a declarative HTML page into a PORTAL. Because of the large number of pages, we adopt Dynamic rewriting. The purpose of this article is not to introduce our project. So let's talk about my script directly. Because of our work, we need to perform simple tag Analysis on static pages before performing operations. Here we mainly analyze TABLE, TR and TD.

Next, paste my code: Python beginners mainly have two files:

 
 
  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) 

I'm a beginner in Python. The above program is very messy, and you will have time to modify or add some comments later. Of course, you are welcome to give your comments. However, the final result is that a total of 1000 of our 200 statement pages contain pages. These three labels have errors. This means there are a lot of things to deal. We have not made a decision on how to do it.

  1. How to embed Python into C ++ applications?
  2. In-depth discussion of Ruby and Python syntax comparison
  3. Introduction to Python
  4. Python Learning Experience: version, IDE selection and coding Solutions
  5. Analysis of Python GIL and thread security

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.