Python 常用代码片段

[python]  view plain copy
  1. 1.生成随机数  
  2.           import random    #这个是注释,引入模块  
  3.           rnd random.randint(1,500)#生成1-500之间的随机数  
  4.   
  5. 2.读文件  
  6.   
  7.          open("c:\\1.txt","r" 
  8.          lines f.readlines()#读取全部内容  
  9.          for line in lines  
  10.                  print line  
  11. 3.写文件  
  12.         open("c:\\1.txt","r+")#可读可写模式  
  13.         f.write("123")#写入字符串  
  14.   
  15. 4.正则表达式,读取tomcat的日志并打印日期  
  16.   
  17.      import re  
  18.      regx "\d\d\d\d-\d\d-\d+"  
  19.      open("c:\stdout.log","r" 
  20.      0  
  21.      for str in f.readlines():  
  22.         if re.search(regx,str):  
  23.              Response.write(str+"
    "
     
  24.               if i>10:break#由于是测试,只分析十行  
  25.               i=i+1  
  26.      f.close();  
  27.   
  28. 5.连接数据库  
  29.   
  30. import pgdb  
  31.   
  32. conn pgdb.connect  
  33.   
  34. (host='localhost',databse='qingfeng',user='qingfeng',password='123' 
  35.   
  36.         cur conn.cursor()   
  37.   
  38.         cur.execute("select from dream"  
  39.   
  40.         print cur.rowcount  
  41.   
  42. 6.SAX处理xml:  
  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(selfname, attrs):  
  58.               if name == 'quotation' 
  59.                   print 'QUOTATION:'  
  60.                   self.in_quote 1  
  61.               else 
  62.                   self.thisquote self.thisquote '{'  
  63.   
  64.           def endElement(selfname):  
  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(selfch, 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.setDocumentHandler(handler)  
  81.           parser.parseFile(open("sample.xml"))  
  82.           parser.close()  
  83.   
  84.   
  85. 7.python的GUI模块标准的是Tkinter,也有QT和MFC的模块,有兴趣的大家自己搜索下  
  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()  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值