收集的一些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:/stdout.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()

  • 0
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python是一种功能强大且易于学习的编程语言,拥有非常有趣实用的应用程序。以下是一些例子: 1. 游戏开发:Python的游戏库如Pygame和Pyglet使得开发游戏变得简单有趣。你可以创建各种类型的游戏,从文字冒险到二维平台游戏。 2. 数据分析:Python具有广泛的数据分析工具和库,如NumPy、Pandas、Matplotlib和SciPy等。通过这些工具,你可以对数据进行统计分析、可视化和建模。 3. 网络爬虫:使用Python编写网络爬虫可以帮助你自动从互联网上获取信息。你可以爬取网页内容、从API中抓取数据或者下载文件等。 4. 自动化任务:Python可以轻松实现各种自动化任务,例如文件重命名、批处理处理、网站自动登录等。你可以编写脚本来简化繁琐的重复任务。 5. 机器学习:Python拥有丰富的机器学习库如Scikit-Learn和TensorFlow等。你可以使用Python编写机器学习算法来解决各种问题,如图像识别、文本分类等。 6. 命令行实用工具:Python可以用来编写命令行实用工具,例如文件搜索、文本处理和系统管理等。这些工具可以提高你的效率,并简化一些复杂的操作。 总之,Python有很多实用有趣程序,而且由于它的易学性和强大的生态系统,你可以轻松找到各种用途的应用。无论你是从事数据科学、网络开发、自动化还是机器学习等领域,Python都能够满足你的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值