快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

收集的一些python程序,实用并且有趣


这几天有空就做做python,下面是我收集的一些代码,大家可以看看,python真的很有有趣!

1。生成随机数           import random    #这个是注释,引入模块          rnd = random.randint(1,500)#生成1-500之间的随机数

2。读文件

         f = open("c://1.txt","r")          lines = f.readlines()#读取全部内容          for line in lines                  print line  

3。 写文件         f = open("c://1.txt","r+")#可读可写模式         f.write("123")#写入字符串

4。正则表达式,读取tomcat的日志并打印日期

     import re      regx = "/d/d/d/d-/d/d-/d+"      f = open("c: tdout.log","r")      i = 0      for str in f.readlines():         if re.search(regx,str):              Response.write(str+"<br>")               if i>10:break#由于是测试,只分析十行               i=i+1      f.close();

5。连接数据库

         import pgdb

        conn = pgdb.connect(host=\\\'localhost\\\',databse=\\\'qingfeng\\\',user=\\\'qingfeng\\\',password=\\\'123\\\')

        cur = conn.cursor() 

        cur.execute("select * from dream") 

        print cur.rowcount 6。还有几个网络编程和RPC的例子,代码太长,详细见http://www.javaeye.com/viewtopic.php?t=10115

7。SAX处理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的GUI模块标准的是Tkinter,也有QT和MFC的模块,有爱好的大家自己搜索下

        import Tkinter

        root=Tkinter.Tk()

        myLabel(root,"Welcome to python\\\'s world")

        myLabel.pack()

        root.mainloop()




返回类别: 教程
上一教程: 正则表达式在javascript中的几个实例2
下一教程: 面向对象的设计与实现的一些基础但重要的概念

您可以阅读与"收集的一些python程序,实用并且有趣"相关的教程:
· 网上收集的一些Java应用
· ibm developer的一些对我有用的网址
· Java的一些类的使用经验
· Java学习过程应该深入理解的一些重点
· 用JSP实现的一个日历程序
    微笑服务 优质保证 索取样品