Python Note
Python 中文官方文檔
str
- str.translate(table [,deletechars]) -> string
>>> from string import punctuation
>>> s = 'abc,./123'
>>> s.translate(None , punctuation)
'abc123'
>>> s.translate('a'* , punctuation)
'aaaaaa'
- str.startswith(prefix[, start[, end]]) -> bool
- str.endswith(suffix[, start[, end]]) -> bool
>>> s = 'id=asd123'
>>> s.startswith('id=')
True
>>> s.endswith('id=')
False
- str.split([sep [,maxsplit]]) -> list of strings
>>> s='1&2&3&4'
>>> s.split('&')
['1', '2', '3', '4']
>>> s.split('&',)
['1', '2&3&4']
>>> s.split('&',)
['1', '2', '3', '4']
>>> s.split('&',)
['1', '2', '3', '4']
- str.center(width[, fillchar]) -> string
>>> s = 'asd123'
>>> s.center()
' asd123 '
>>> str_list = [' '.join(map(str,range(,i+))) for i in range(,)]
>>> str_list
['1', '1 2', '1 2 3', '1 2 3 4']
>>> for s in str_list:
... print s.center()
...
- str.expandtabs([tabsize]) -> string
>>> s='1\t2\t3'
>>> s.expandtabs() #default 8
'1 2 3'
>>> s.expandtabs()
'1 2 3'
- * *
string
- string.punctuation
>>> from string import punctuation
>>> punctuation
'!"#$%&\'()*+,-./:;<=>[email protected][\\]^_`{|}~'
operator
>>> import operator as op
- op.itemgetter
>>> mylist = [('a',),('c',),('b',)]
>>> sorted(mylist,key=op.itemgetter()) #sort by col2
[('c', ), ('a', ), ('b', )]
>>> sorted(mylist,key=op.itemgetter()) #sort by col1
[('a', ), ('b', ), ('c', )]
time
import time
>>> cur_timestamp = time.time()#目前時間戳
>>> cur_time = time.localtime()#目前時間
time.struct_time(tm_year=, tm_mon=, tm_mday=, tm_hour=, tm_min=, tm_sec=, tm_wday=, tm_yday=, tm_isdst=)
>>> str_time = time.ctime()#目前時間(預設str格式)
'Thu Feb 25 14:27:54 2016'
>>> time.strftime("%Y-%m-%d")#目前時間(自定義str格式)
'2016-02-25'
字元串 ⟹ 時間 ⟹ 時間戳
import time
str_tm = "2016-02-25 14:30:24"
tm = time.strptime(str_tm,"%Y-%m-%d %H:%M:%S")
tm_stamp = time.mktime(tm)
print 'tm =' , tm
print 'tm_stamp =' , tm_stamp
"""
tm = time.struct_time(tm_year=2016, tm_mon=2, tm_mday=25, tm_hour=14, tm_min=30, tm_sec=24, tm_wday=3, tm_yday=56, tm_isdst=-1)
tm_stamp = 1456381824.0
"""
時間戳 ⟹ 時間 ⟹ 字元串
import time
tm_stamp =
tm = time.gmtime(tm_stamp)
str_tm = time.strftime("%Y-%m-%d %H:%M:%S",tm)
print 'tm =' , tm
print 'str_tm =' , str_tm
"""
tm = time.struct_time(tm_year=2016, tm_mon=2, tm_mday=25, tm_hour=6, tm_min=30, tm_sec=24, tm_wday=3, tm_yday=56, tm_isdst=0)
str_tm = 2016-02-25 06:30:24
"""
計時
>>> begin=time.time();time.sleep();time.time()-begin
注:在unix like上使用 time.time() 而在windows中使用time.clock()可以得到更高的精度.

圖檔引自:http://blog.csdn.net/five3/article/details/8771612
datetime
- 詳見:http://my.oschina.net/whp/blog/130710
MAXYEAR && MINYEAR
In []: datetime.MAXYEAR
Out[]:
In []: datetime.MINYEAR
Out[]:
date
In []: datetime.date.today()
Out[]: datetime.date(, , )
In []: datetime.date.today().year#month,day:2,25
Out[]:
In []: datetime.date.today().weekday()
Out[]:
datetime
In []: datetime.datetime.today()
Out[]: datetime.datetime(, , , , , , )
In []: datetime.datetime.today().year
Out[]:
In []: datetime.datetime.today().hour
Out[]:
In []: datetime.datetime.today().timetuple()
Out[]: time.struct_time(tm_year=, tm_mon=, tm_mday=, tm_hour=, tm_min=, tm_sec=, tm_wday=, tm_yday=, tm_isdst=-)
In []: datetime.datetime.strftime(datetime.datetime.today(),"%Y-%m-%d %H:%M:%S")
Out[]: '2016-02-25 18:23:53'
In []: datetime.datetime.strptime('2016-02-25 18:23:53',"%Y-%m-%d %H:%M:%S")
Out[]: datetime.datetime(, , , , , )
注:datetime.datetime.strftime(datetime.datetime.today(),”%Y-%m-%d %H:%M:%S”)
== time.strftime(“%Y-%m-%d %H:%M:%S”)
timedelta
random
Python中的random子產品