python常用模块及方法二

上一篇 / 下一篇  2016-05-20 20:19:10 / 个人分类:python

一.日期时间处理:

DC#K#\.UT0

 datetime库51Testing软件测试网"|8}u#^ G-P

>>> import datetime51Testing软件测试网`/}fLWL"I:Oi

>>> datetime.datetime.now()  #当前时间

~:Y$fqXp7{1Q0

datetime.datetime(2016, 4, 16, 12, 53, 43, 104000)

D}p a;A@.M G0

>>> datetime.datetime.today() #当前时间51Testing软件测试网zSM-Nf1W:}9z'[r

datetime.datetime(2016, 4, 16, 12, 54, 6, 647000)

^otnWA0

>>> datetime.datetime.strftime(datetime.datetime.now(),'%Y-%m-%d')

A Y^p1m6F0

'2016-04-16'#时间转换成指定格式字符串51Testing软件测试网$j(K-l.T2WH.^

>>> datetime.datetime.strftime(datetime.datetime.now(),'%Y%m%d%H%M%S')51Testing软件测试网[&\"l~?|1q(Vp

'20160416125604''#时间转换成指定格式字符串,常用的时间戳

oo&A_5m8W \v0

>>> datetime.datetime.strptime('2015-03-12',"%Y-%m-%d %H:%M:%S")

9Mhv4K4R"a7n0

#字符串转成日期51Testing软件测试网 i(SCDB!D

>>> datetime.datetime.strptime('2015-03-12',"%Y-%m-%d")51Testing软件测试网yS4^2f-]I6Uk+f

datetime.datetime(2015, 3, 12, 0, 0)51Testing软件测试网p2u/W9OYF4N'Bwy

 

I:c?aL0

#日期相加减51Testing软件测试网 |;z rI_!T Bc

>>> datetime.datetime.now()+datetime.timedelta(days=2)

+wI VX0k1S5} Ha `0

datetime.datetime(2016, 4, 18, 13, 0, 47, 197000)51Testing软件测试网mcT}%h%X w

>>> datetime.datetime.now()+datetime.timedelta(hours=2)

zA9I R%I5b^!cO0

datetime.datetime(2016, 4, 16, 15, 1, 11, 417000)

({]8}lgh%dI0

#日期的格式化,

*^Z IB(f \4MX0

>>> d=datetime.datetime.now()51Testing软件测试网+h;x.\ Ty7w-Xh

>>> print d.strftime('%a')51Testing软件测试网R p[(u,eJ'`hQ

Sat #星期简写

}l'c ^ukjE0

>>> print d.strftime('%A')

(JK-wd*};`w0

Saturday #月份简写51Testing软件测试网+VtKuP%M9Rf%}

>>> print d.strftime('%B')51Testing软件测试网*M0eo#A-i;|

April51Testing软件测试网'bB3H;{XPOSj

>>> print d.strftime('%w')51Testing软件测试网.?D&v5HO$B&{

6 #星期几51Testing软件测试网d+c/fA-F%m5V

>>> print d.strftime('%W')51Testing软件测试网.r4cFUz fO|I6z*bp

15#当前周在一年的第几周51Testing软件测试网u'M;kn+z

>>> print d.strftime('%p')51Testing软件测试网$B"E.Y5p_

PM51Testing软件测试网 u6]d6i-|2J$c

calendar库:日历月历51Testing软件测试网gR#I0N7N J)l8`/@

import calendar

qCbXV\0nS HZ0

calendar.prmonth(1999, 12)

/z4X.|?2a0

 

0r:lOL{'|@r2a7r:Q0

二.操作excel

6Qr-K`7A r3qpt0

需要安装使用xlrd库,51Testing软件测试网|&m:m5^ JC

>>> import xlrd

.g]9h+d:B:F0

>>> xls=xlrd.open_workbook('e://test.xls')

j`4V@z gD0

>>> table=xls.sheets()[0]51Testing软件测试网D[;U;B1y.B.ec

>>> table.cell(0,0).values()51Testing软件测试网*U$dEe'M:T3q7`Abi

>>> table.row_values(1)51Testing软件测试网d[8V_4J d Xo*?.qU

[4.0, 5.0, 6.0]#第二行数据,list

-P+G5qa D*io0

#编辑打印每一行数据51Testing软件测试网 |d E.X o

>>> for i in range(table.nrows):51Testing软件测试网!{$X"`!G,tM D8l[O

     print table.row_values(i)

:O` UCY6M;N0

[1.0, 2.0, 3.0]51Testing软件测试网0l;g5~(l$rGz0W7h

[4.0, 5.0, 6.0]

tY6dV"G)h{f0B0

 

TMv:G,w'Qj0

三.操作xml51Testing软件测试网_#v q T~M}

>>> import xml.dom.minidom as xmldom51Testing软件测试网8lN0[@!d8bA

>>> dom=xmldom.parse('test.xml')

z R _!]auy0

>>> root = dom.documentElement51Testing软件测试网"W W.i&H p3^C

>>>idnode=root.getElementsByTagName('id')#根据元素名字获取元素,如果得到多个元素可根据下标来获取具体的元素

3fK$d%S+L] H^0

>>>childs=idnode.childNodes #获取子元素集合51Testing软件测试网n9o'|Ho,}
>>>idnode[0].getAttribute('attrname')#获取元素属性

l hh$T{1S*dpM"d0

 51Testing软件测试网B?'yg9P

四.操作csv文件51Testing软件测试网'ApR Z3R

import csv51Testing软件测试网:V^qi,K"y#N

>>>read = csv.reader(file('your.csv', 'rb'))

IpY1U Y'?%X-DL0

>>> for line in read:51Testing软件测试网!].s(AJ3zX2h6uw

     for i in line:

Q!Q4tv)?2F0

          print i51Testing软件测试网#l.Ru@G_tv/a9~

写csv文件

cH*|o x_P.{0

csvfile = file('csv_test.csv', 'wb')51Testing软件测试网,|?!fAt

writer = csv.writer(csvfile)51Testing软件测试网]ia;e3G_PR

writer.writerow(['姓名', '年龄', '电话'])51Testing软件测试网-I%c%b l;M]2j4c4E2g3m2g

data = [……]#可以直接写list51Testing软件测试网X%M'y!YM|Tm7U*j

writer.writerows(data)

4ml0U%OM6] C L%f0

csvfile.close()51Testing软件测试网+mw;uk'f@

51Testing软件测试网;pbR(aeZ`[
51Testing软件测试网,r9A%rBU

五.操作sqlite351Testing软件测试网!Bdf!n p"`3Y"W

Sqlite是一种轻量级的数据库,sqlite3在目前的Python2.5以上版本已经集成51Testing软件测试网8rJLr;~\d.|E.I6V

>>> import sqlite3

jQ$x0Sj2i'kQ0

>>> sqlite3.connect('test1.db') #如果不存在,则新建,创建后则会生成*.db文件

,J*v3nQ6aw/E9dr8G0

<sqlite3.Connection object at 0x02C27E30>51Testing软件测试网6z3b'W R`JQ:d

>>> conn=sqlite3.connect('test1.db')51Testing软件测试网9e/PeRAH I'E^

>>> cur=conn.cursor() #创建一个游标

U0L%r2u~3k}a0

>>> cur.execute('create table mytable(id integer,value text)')

6Kc'a tR w w2D0

>>> cur.execute("INSERT INTO mytable(id,value) values(1,'feiyu')")51Testing软件测试网i[&e)Qv

<sqlite3.Cursor object at 0x02C6D060>51Testing软件测试网2K z~W_7E

>>> conn.commit()  #提交事务51Testing软件测试网@q0FL?4K

>>> result=cur.execute('select * from mytable')51Testing软件测试网9Ga V geb*?/a {

>>> print result.fetchall() #获取查询结果中所有的行51Testing软件测试网/RS(N x1k?'G4`/tD

[(1, u'feiyu')]

HvwrX#D0

>>>Conn.Close()  #关闭连接51Testing软件测试网N%L@;b)j'?5z


TAG: Python python

 

评分:0

我来说两句

Open Toolbar