Common Python code snippets

Source: Internet
Author: User
[Code] [Python] Code
01 1. Generate a random number
02 import random # This is a comment, introducing a module
03 RND = random. randint (1,500) # generate a random number between 1 and
04
05 2. Read files
06
07 F = open ("C: \ 1.txt"," R ")
08 lines = f. readlines () # Read all content
09 for line in lines
10 print line
11 3. Write files
12 F = open ("C: \ 1.txt"," R + ") # readable/writable Mode
13 F. Write ("123") # Write a string
14
4. Regular Expression, read Tomcat logs and print the date
16
17 import re
18 regx = "\ D-\ D +"
19 F = open ("C: \ stdout. log", "R ")
20 I = 0
21 For STR in F. readlines ():
22 if Re. Search (regx, STR ):
23 Response. write (str + "<br> ")
24 if I> 10: break # because it is a test, only 10 rows are analyzed.
25 I = I + 1
26 f. close ();
27
28 5. Connect to the database
29
30 import pgdb
31
32 conn = pgdb. connect
33
34 (host = 'localhost', databse = 'qingfeng', user = 'qingfeng', password = '000000 ')
35
36 cur = conn. cursor ()
37
38 cur.exe cute ("select * From dream ")
39
40 print cur. rowcount
41
42 6. Process XML with Sax:
43
44 import string
45 from XML. Sax import saxlib, saxexts
46
47 class quotationhandler (saxlib. handlerbase ):
48 "crude sax Extractor for quotations. DTD document """
49
50 def _ init _ (self ):
51 self. in_quote = 0
52 self. thisquote =''
53
54 def startDocument (self ):
55 print '--- Begin Document ---'
56
57 def startElement (self, name, attrs ):
58 if name = 'quotation ':
59 print 'quotation :'
60 self. in_quote = 1
61 else:
62 self. thisquote = self. thisquote + '{'
63
64 def endElement (self, name ):
65 if name = 'quotation ':
66 print string. join (string. split (self. thisquote [: 230]) + '...',
67 print '(' + str (len (self. thisquote) + 'bytes) \ N'
68 self. thisquote =''
69 self. in_quote = 0
70 else:
71 self. thisquote = self. thisquote + '}'
72
73 def characters (self, ch, start, length ):
74 if self. in_quote:
75 self. thisquote = self. thisquote + ch [start: start + length]
76
77 if _ name _ = '_ main __':
78 parser = saxexts. XMLParserFactory. make_parser ()
79 handler = QuotationHandler ()
80 parser. setincluenthandler (handler)
81 parser. parseFile (open ("sample. xml "))
82 parser. close ()
83
84
85 7. python's GUI module standard is Tkinter, and there are also QT and MFC modules. If you are interested, search for them by yourself.
86
87 import Tkinter
88
89 root = Tkinter. Tk ()
90
91 my = Label (root, "Welcome to python's world ")
92
93 my. pack ()
94
95 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.