Python Note
str
- str.translate(table [,deletechars]) -> string
>>> from string import punctuation
>>> s = 'abc,./123'
>>> s.translate(None , punctuation)
'abc123'
>>> s.translate('a'*256 , 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)
['1', '2&3&4']
>>> s.split('&',3)
['1', '2', '3', '4']
>>> s.split('&',4)
['1', '2', '3', '4']
- str.center(width[, fillchar]) -> string
>>> s = 'asd123'
>>> s.center(20)
' asd123 '
>>> str_list = [' '.join(map(str,range(1,i+1))) for i in range(1,5)]
>>> str_list
['1', '1 2', '1 2 3', '1 2 3 4']
>>> for s in str_list:
... print s.center(20)
...
1
1 2
1 2 3
1 2 3 4
- str.expandtabs([tabsize]) -> string
>>> s='1\t2\t3'
>>> s.expandtabs() #default 8
'1 2 3'
>>> s.expandtabs(4)
'1 2 3'
- * *
string
- string.punctuation
>>> from string import punctuation
>>> punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
operator
>>> import operator as op
- op.itemgetter
>>> mylist = [('a',2),('c',1),('b',3)]
>>> sorted(mylist,key=op.itemgetter(1)) #sort by col2
[('c', 1), ('a', 2), ('b', 3)]
>>> sorted(mylist,key=op.itemgetter(0)) #sort by col1
[('a', 2), ('b', 3), ('c', 1)]
time
import time
>>> cur_timestamp = time.time()#当前时间戳
1456381674.0
>>> cur_time = time.localtime()#当前时间
time.struct_time(tm_year=2016, tm_mon=2, tm_mday=25, tm_hour=14, tm_min=27, tm_sec=54, tm_wday=3, tm_yday=56, tm_isdst=0)
>>> 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 = 1456381824.0
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(3);time.time()-begin
3.003235101699829
注:在unix like上使用 time.time() 而在windows中使用time.clock()可以得到更高的精度.
图片引自:http://blog.csdn.net/five3/article/details/8771612
datetime
MAXYEAR && MINYEAR
In [105]: datetime.MAXYEAR
Out[105]: 9999
In [106]: datetime.MINYEAR
Out[106]: 1
date
In [123]: datetime.date.today()
Out[123]: datetime.date(2016, 2, 25)
In [124]: datetime.date.today().year#month,day:2,25
Out[124]: 2016
In [125]: datetime.date.today().weekday()
Out[125]: 3
datetime
In [136]: datetime.datetime.today()
Out[136]: datetime.datetime(2016, 2, 25, 18, 19, 41, 638621)
In [137]: datetime.datetime.today().year
Out[137]: 2016
In [138]: datetime.datetime.today().hour
Out[138]: 18
In [139]: datetime.datetime.today().timetuple()
Out[139]: time.struct_time(tm_year=2016, tm_mon=2, tm_mday=25, tm_hour=18, tm_min=20, tm_sec=35, tm_wday=3, tm_yday=56, tm_isdst=-1)
In [141]: datetime.datetime.strftime(datetime.datetime.today(),"%Y-%m-%d %H:%M:%S")
Out[141]: '2016-02-25 18:23:53'
In [142]: datetime.datetime.strptime('2016-02-25 18:23:53',"%Y-%m-%d %H:%M:%S")
Out[142]: datetime.datetime(2016, 2, 25, 18, 23, 53)
注:datetime.datetime.strftime(datetime.datetime.today(),”%Y-%m-%d %H:%M:%S”)
==
time.strftime(“%Y-%m-%d %H:%M:%S”)