1\生成随机数
import random #引入模块
rnd=random.randint(1,100) #生成1-500间的随机数
2\读文件
f=open("c:\\1.txt","r")
lines=f.readlines()#读取全部内容
for line in lines:
print line
3\写文件
f=open("c://2.txt","w")
f.write("1111111")
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\连接微软数据库、oracle
import pgdb
conn=pgdb.connect
(host='localhost',database='databasename',user='name',password='123')
cur=conn.cursor()
cur.execute("select * from tablename")
print cur.rowcount
import cx_Oracle
o=cx_Oracle.connect('username','password','url:port/sid')
cur=o.cursor()
cur.excute('select * from tablename')
for row in cur:
print row
cur.description
6\SAX处理xml
import string
from xml.sax import saxlib,saxexts
class QuotationHander(saxlib.HandlerBase):
""" ... """
def _int_(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=QuotationHander()
parser.setDocumentHandler(handler)
parser.parsefile(open("sample.xml"))
parser.close()
7\关于python时间模块问题
#:当前时间时间戳 1312181113.31
print(time.time())
#将字符串转成时间戳
ts = '2011-08-01 14:15:40'
b = time.mktime(time.strptime(ts,'%Y-%m-%d %H:%M:%S'))
print(b)
#返回指定时间的时间戳使用mktime
d = datetime.datetime(1997,12,29,15,59,59)
t = d.timetuple()#再转为元组
print(time.mktime(t)) #使用time的mktime方法返回时间戳
#将时间戳转成时间使用strftime()
u = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.mktime(t)))
print(u)
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(b)))
#当前时间 2011-08-01 14:44:32.640000
print(datetime.datetime.now())
#或者:
print(time.strftime('%Y-%m-%d %H:%M:%S'))
以例子需要说明:
strftime(format[, tuple]) -> string
将指定的struct_time(元组格式的)(默认为当前时间),根据指定的格式化字符串输出
python中时间日期格式化符号:
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身
import random #引入模块
rnd=random.randint(1,100) #生成1-500间的随机数
2\读文件
f=open("c:\\1.txt","r")
lines=f.readlines()#读取全部内容
for line in lines:
print line
3\写文件
f=open("c://2.txt","w")
f.write("1111111")
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\连接微软数据库、oracle
import pgdb
conn=pgdb.connect
(host='localhost',database='databasename',user='name',password='123')
cur=conn.cursor()
cur.execute("select * from tablename")
print cur.rowcount
import cx_Oracle
o=cx_Oracle.connect('username','password','url:port/sid')
cur=o.cursor()
cur.excute('select * from tablename')
for row in cur:
print row
cur.description
6\SAX处理xml
import string
from xml.sax import saxlib,saxexts
class QuotationHander(saxlib.HandlerBase):
""" ... """
def _int_(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=QuotationHander()
parser.setDocumentHandler(handler)
parser.parsefile(open("sample.xml"))
parser.close()
7\关于python时间模块问题
#:当前时间时间戳 1312181113.31
print(time.time())
#将字符串转成时间戳
ts = '2011-08-01 14:15:40'
b = time.mktime(time.strptime(ts,'%Y-%m-%d %H:%M:%S'))
print(b)
#返回指定时间的时间戳使用mktime
d = datetime.datetime(1997,12,29,15,59,59)
t = d.timetuple()#再转为元组
print(time.mktime(t)) #使用time的mktime方法返回时间戳
#将时间戳转成时间使用strftime()
u = time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.mktime(t)))
print(u)
print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(b)))
#当前时间 2011-08-01 14:44:32.640000
print(datetime.datetime.now())
#或者:
print(time.strftime('%Y-%m-%d %H:%M:%S'))
以例子需要说明:
strftime(format[, tuple]) -> string
将指定的struct_time(元组格式的)(默认为当前时间),根据指定的格式化字符串输出
python中时间日期格式化符号:
%y 两位数的年份表示(00-99)
%Y 四位数的年份表示(000-9999)
%m 月份(01-12)
%d 月内中的一天(0-31)
%H 24小时制小时数(0-23)
%I 12小时制小时数(01-12)
%M 分钟数(00=59)
%S 秒(00-59)
%a 本地简化星期名称
%A 本地完整星期名称
%b 本地简化的月份名称
%B 本地完整的月份名称
%c 本地相应的日期表示和时间表示
%j 年内的一天(001-366)
%p 本地A.M.或P.M.的等价符
%U 一年中的星期数(00-53)星期天为星期的开始
%w 星期(0-6),星期天为星期的开始
%W 一年中的星期数(00-53)星期一为星期的开始
%x 本地相应的日期表示
%X 本地相应的时间表示
%Z 当前时区的名称
%% %号本身