Python important block of function code

Source: Internet
Author: User
Tags print print

Note: The print print statement in python3.3.5 now has a new wording:

1. Python specifies that random numbers are generated

Import Random # correctly test print notation >>> rand = random.randint (1,500)print  (rand)#  Wrong print syntax:print  randsyntaxerror:invalid syntax

2. #python对文件操作

1. Generate random NumbersImportRandom#This is a note that introduces the moduleRnd = Random.randint (1,500)#generate a random number between 1-5002. Read the file F= Open ("C:\\1.txt","R") Lines= F.readlines ()#Read all content          forLineinchLinesPrint Line3. write file F= Open ("C:\\1.txt","r+")#Readable writable modeF.write ("123")#Write String4. Regular expressions, reading Tomcat logs and printing datesImportre regx="\d\d\d\d-\d\d-\d+"F= Open ("C:\stdout.log","R") I=0 forStrinchf.readlines ():ifRe.search (REGX,STR): Response.Write (str+"<br>")              ifI&GT;10: Break#because it is a test, only 10 rows are analyzedI=i+1f.close ();5. Connect to the databaseImportPgdb Conn=Pgdb.connect (Host='localhost', databse='Qingfeng', user='Qingfeng', password='123') cur=conn.cursor () Cur.execute ("select * FROM Dream")         PrintCur.rowcount6. Sax Processes xml:Importstring fromXml.saxImportSaxlib, SaxextsclassQuotationhandler (saxlib. Handlerbase):"""Crude sax Extractor for QUOTATIONS.DTD document"""           def __init__(self): Self.in_quote=0 Self.thisquote="'           defstartdocument (self):Print '---Begin Document---'           defstartelement (self, Name, attrs):ifName = ='Quotation':                  Print 'Quotation:'Self.in_quote= 1Else: Self.thisquote= Self.thisquote +'{'           defendElement (self, name):ifName = ='Quotation':                  PrintString.Join (String.Split (self.thisquote[:230)) +'...',                  Print '('+str (Len (self.thisquote)) +'bytes) \ n'Self.thisquote="'Self.in_quote=0Else: Self.thisquote= Self.thisquote +'}'           defcharacters (self, ch, start, length):ifSelf.in_quote:self.thisquote= Self.thisquote + ch[start:start+Length]if __name__=='__main__': Parser=saxexts. Xmlparserfactory.make_parser () handler=Quotationhandler () Parser.setdocumenthandler (handler) parser.parsefile (open ("Sample.xml") ) Parser.close ()7The GUI module standard for Python is Tkinter, also has Qt and MFC modules, which are interested in everyone's own search underImportTkinter Root=tkinter.tk () my=label (Root,"Welcome to Python 's world") My.pack () Root.mainloop ()

Python important block of function code

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.