python常用模块及方法二

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

一.日期时间处理:

hoh^Ie$U0

 datetime库

hS$J8e,y$US0

>>> import datetime51Testing软件测试网 is(c(K0jw._

>>> datetime.datetime.now()  #当前时间51Testing软件测试网x7UpQ6h$r dE

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

Wj6BB ]0

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

E mJ0p+g$D0

datetime.datetime(2016, 4, 16, 12, 54, 6, 647000)51Testing软件测试网__iX/o#v

>>> datetime.datetime.strftime(datetime.datetime.now(),'%Y-%m-%d')51Testing软件测试网5zsx1K.g

'2016-04-16'#时间转换成指定格式字符串51Testing软件测试网Ij ~0L/N?

>>> datetime.datetime.strftime(datetime.datetime.now(),'%Y%m%d%H%M%S')

f r{(VIE*D0

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

YEu]cqh+q;W0

>>> datetime.datetime.strptime('2015-03-12',"%Y-%m-%d %H:%M:%S")51Testing软件测试网gsi*G0D+m6qwY9D1L

#字符串转成日期51Testing软件测试网*g6r+D.P%|^

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

oV'BX0L0h t/e0

datetime.datetime(2015, 3, 12, 0, 0)

D`.C~Vrx?0

 

3pR gS5vn0

#日期相加减

,h\3G#X0Y&b5}?0

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

4h)Gq}^0p0

datetime.datetime(2016, 4, 18, 13, 0, 47, 197000)51Testing软件测试网/mACm |4vp.E:]

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

0N)M;Vk6c^0

datetime.datetime(2016, 4, 16, 15, 1, 11, 417000)51Testing软件测试网h bty} @ u8Z&Eb!H

#日期的格式化,51Testing软件测试网+Y3O5bI J6aAw![

>>> d=datetime.datetime.now()

']B @fk9MY0

>>> print d.strftime('%a')51Testing软件测试网$j$]*iHe3D

Sat #星期简写

'S/t u:TkD6~0

>>> print d.strftime('%A')51Testing软件测试网5E.{z$\-^sTD'^ThE

Saturday #月份简写51Testing软件测试网1Vp[ B%y

>>> print d.strftime('%B')51Testing软件测试网:q'T9W K9I

April

u^wEjEXZ&YL0

>>> print d.strftime('%w')51Testing软件测试网-r'S Op e}G t:U

6 #星期几51Testing软件测试网v%U Y:TW)De

>>> print d.strftime('%W')51Testing软件测试网+j,Xk:u K+a,`

15#当前周在一年的第几周

GL@0Fn@ ou `+p&e0

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

_JH,U,i-o&X0

PM

(Sp8rzIB7l-R0

calendar库:日历月历51Testing软件测试网 W ?t&l^

import calendar

u?ojij2I:S/g!w0

calendar.prmonth(1999, 12)

0b(p$@]F EV7w-S h0

 51Testing软件测试网 ix H [&| ec*q9B

二.操作excel

#n*f4o[8O x0

需要安装使用xlrd库,

%S/s_/Io0

>>> import xlrd51Testing软件测试网p|(P;zrLJt9B5cS

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

1V$qdD+S(i G0

>>> table=xls.sheets()[0]

k|)f+C,Og/c0

>>> table.cell(0,0).values()51Testing软件测试网_R2@:aV)OX4PD

>>> table.row_values(1)51Testing软件测试网]"r%_k#T:Z^

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

D,f7vq,Y.CN0@ x-s0

#编辑打印每一行数据

6Em)HQ6Aq0

>>> for i in range(table.nrows):51Testing软件测试网/OXL OP.W3N+m'{o

     print table.row_values(i)

I%YXu;BI0

[1.0, 2.0, 3.0]51Testing软件测试网!} wxb{

[4.0, 5.0, 6.0]

F2d-Sng I9t0

 51Testing软件测试网S#[.U ^,@9xa3E+}5l rI

三.操作xml51Testing软件测试网G,^ T?t#?)m

>>> import xml.dom.minidom as xmldom51Testing软件测试网%ICb2HM/T'Q

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

c0`e5q[Uz;Z0

>>> root = dom.documentElement

G(jedv+K8Q3M:i0

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

a.al4q'|_nZk0

>>>childs=idnode.childNodes #获取子元素集合
dY$|K5DB1kDWd0>>>idnode[0].getAttribute('attrname')#获取元素属性

La)`%bW$Fbe0

 

V3e}YF6[0

四.操作csv文件51Testing软件测试网2^@/]`!cG9r

import csv

7X1A/[\ k9uN:J0

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

-j/Q8E5?[PFd8g0

>>> for line in read:

k i/s'qc0

     for i in line:51Testing软件测试网\)NuL-f mZ|

          print i

{r7x.q6^ K~ n Nf-LiY$O0

写csv文件

EVO rp Y4r%h0

csvfile = file('csv_test.csv', 'wb')51Testing软件测试网:^.L qM8w7r

writer = csv.writer(csvfile)

{+p*i;@T0

writer.writerow(['姓名', '年龄', '电话'])

%V-i \2X k0

data = [……]#可以直接写list

1P/? {1I3_.?,o0

writer.writerows(data)

e'sgT*~ x;r0

csvfile.close()51Testing软件测试网9L'KB6sRS+~I

51Testing软件测试网7cQA%P AQ9{
51Testing软件测试网j z(gH)e)O

五.操作sqlite3

-y4Wf&c$r t]y9~0

Sqlite是一种轻量级的数据库,sqlite3在目前的Python2.5以上版本已经集成51Testing软件测试网5j3c*\h%f5@/m B

>>> import sqlite351Testing软件测试网*n-HVN3v1Y2d G

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

2U8d-]2_ x3D1J.TH J0

<sqlite3.Connection object at 0x02C27E30>

s+XS0C"u Q.a^2oE)I0

>>> conn=sqlite3.connect('test1.db')51Testing软件测试网X({ QlA(r0I;|;m

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

*Z%V!Z*Y@0

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

2Mbu+G-GJ4S,d0

>>> cur.execute("INSERT INTO mytable(id,value) values(1,'feiyu')")51Testing软件测试网6OdmHp Z0K7p

<sqlite3.Cursor object at 0x02C6D060>

F#AU$k+c"c0

>>> conn.commit()  #提交事务51Testing软件测试网j%r3Nj7VY"Z

>>> result=cur.execute('select * from mytable')

2?Q"`X3q6P*l,C g0

>>> print result.fetchall() #获取查询结果中所有的行

q%H&Sm(E*d"W0

[(1, u'feiyu')]

Nr P i jP2l0

>>>Conn.Close()  #关闭连接

fLbpT^/c0

TAG: Python python

 

评分:0

我来说两句

Open Toolbar