博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
常用python编码
阅读量:4680 次
发布时间:2019-06-09

本文共 2373 字,大约阅读时间需要 7 分钟。

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+"
") 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。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()

 

 

7.python的GUI模块标准的是Tkinter,也有QT和MFC的模块,有兴趣的大家自己搜索下

 

import Tkinter        root=Tkinter.Tk()        my=Label(root,"Welcome to python's world")        my.pack()        root.mainloop()

转载于:https://www.cnblogs.com/xupeizhi/archive/2013/02/07/2908880.html

你可能感兴趣的文章
实现局部或全部页面内容不能选中的效果
查看>>
oracle小测试
查看>>
java环境变量
查看>>
1、扩展方法
查看>>
SVN的安装与简单使用
查看>>
LeetCode:平衡二叉树【110】
查看>>
01 操作系统和常用命令
查看>>
Mysql扩展之replication概述
查看>>
C++中数值的后缀
查看>>
EventModify
查看>>
C中int8_t、int16_t、int32_t、int64_t、uint8_t、size_t、ssize_t区别
查看>>
python day2 模块初识、pyc定义
查看>>
一道算法作业题(续)
查看>>
Machine Learning From Scratch-从头开始机器学习
查看>>
基础数据结构
查看>>
python url库学习
查看>>
找“水王”
查看>>
018-伸展树
查看>>
FPM打包工具
查看>>
JDK版本不兼容问题之:一台机器安装多个版本的JDK
查看>>