[Python] common Python code-1

Source: Internet
Author: User
1. generate random number import random # This is a comment, introducing the module RND = random. randint (1,500) # generate a random number between 1 and. read File F = open ("C: \ 1.txt"," R ") lines = f. readlines () # Read all content for line in lines print line3. Write File F = open ("C: \ 1.txt"," R + ") # readable and writable mode f. write ("123") # Write a string 4. regular Expression to read Tomcat logs and print the date import re regx = "\ D-\ D +" F = open ("C: \ stdout. log "," R ") I = 0 for STR in F. readlines (): If re. search (regx, STR): Response. write (STR + "<br>") if I> 10: break # because it is a test, only ten rows of I = I + 1 F are analyzed. close (); 5. import pgdbconn = pgdb. connect (host = 'localhost', Databse = 'qingfeng', user = 'qingfeng', password = '000000') cur = Conn. cursor () cur.exe cute ("select * From dream") print cur. rowcount6.sax processing XML: Import string from XML. sax import saxlib, saxexts class quotationhandler (saxlib. handlerbase): "crude sax Extractor for quotations. DTD document "def _ init _ (Self): Self. in_quote = 0 self. thisquote = ''def startdocument (Self): print '--- begin document --- 'def startelement (self, name, attrs): If name = 'quotation': Print 'quotation: 'self. in_quote = 1 else: Self. thisquote = self. thisquote + '{'def endelement (self, name): If name = 'quotation': print string. join (string. split (self. thisquote [: 230]) + '... ', print' ('+ STR (LEN (self. thisquote) + 'bytes) \ n' self. thisquote = ''self. in_quote = 0 else: Self. thisquote = self. thisquote + '}' def characters (self, CH, start, length): If self. in_quote: Self. thisquote = self. thisquote + CH [start: Start + Length] If _ name _ = '_ main _': parser = saxexts. xmlparserfactory. make_parser () handler = quotationhandler () parser. setincluenthandler (handler) parser. parsefile (open ("sample. XML ") parser. close () 7. python's GUI module standard is tkinter, and there are also QT and MFC modules. If you are interested, search for import tkinter root = tkinter. TK () My = label (root, "Welcome to Python's world") My. pack () root. mainloop ()

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.