I have time to do Python these days, below is some of the code I collected, you can see, Python is really interesting!
1. Generate random number Import Random # This is a comment, introduced module RND = random.randint (1,500) # Generate a random number between 1-500
2. Read file
f = Open ("c: //1.txt", "r") lines = f.readlines () #er reading full content for LINE IN LINES PRINT LINE
3. Write F = Open ("C: //1.txt", "R ") # readable can be Writable mode F.Write ("123") # Write string
4. Regular expressions, read Tomcat logs and print the date
Import rereg = "/ d / d / d / d- / d / d " f = open ("c: /stdout.log", "r") i = 0 for str in f.readlines () : If Re.Search (Regx, Str): Response.write (STR "
") IF i> 10: Break # Because it is test, only 10 lines i = i 1 f.close ();
5. Connect to the database
Import PGDB
Conn = pgdb.connect (Host = 'localhost', Database = 'qingfeng', user = 'qingfeng', password = '123')
Cur = conn.cursor ()
Cur.execute ("SELECT * from DREAM")
Print Cur.rowcount 6. There are also several online programming and RPC examples, the code is too long, see http://www.javaeye.com/viewtopic.php?t=10115
7. 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.setDocumentHandler (handler) parser.parseFile (open ( "sample.xml")) parser.close () 8.python of The GUI module standard is Tkinter, and there is also a module of Qt and MFC. It is interested in searching for yourself.
Import tkinter
root = tkinter.tk ()
Mylabel (root, "Welcome to Python's World)
Mylabel.pack ()
root.mainloop ()