打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python 常用代码片段 - 代码分享 - 开源中国社区
Python 常用代码片段
Sephiroth 发布于 2010年11月15日 13时 (0评) 4人收藏此代码, 我要收藏(?)
一些比较常用的代码片段
标签: python

代码片段(1)

[代码] [Python]代码

01 1.生成随机数
02           import random    #这个是注释,引入模块
03           rnd = random.randint(1,500)#生成1-500之间的随机数
04   
05 2.读文件
06   
07          f = open("c:\\1.txt","r")
08          lines = f.readlines()#读取全部内容
09          for line in lines
10                  print line
11 3.写文件
12         f = 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      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#由于是测试,只分析十行
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(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.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()
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
XML Python 联合编程
python3.6 +tkinter GUI编程 实现界面化的文本处理工具(推荐)
【技能提升】python操作数据库完成接口测试
 python自学
Python GUI项目实战(一)登录窗体的设计与实现
GUI编程:打开文件对话框、保存文件对话框、颜色选择对话框的使用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服