python date物件
>>> from datetime import date
# 創造一個date物件
# date( year, month , day )
>>> d = date( 2008, 1,1 )
>>> d
datetime.date(2008, 1, 1)
# 求( 哪一年,哪一月,哪一天 )
>>> ( d.year , d.month , d.day )
(2008, 1, 1)
# 求星期幾 ( 2008-01-01是星期二 )
>>> d.weekday() , d.isoweekday()
(1, 2)
>>># weekday()是以星期一當0
>>># isoweekday() 是以星期一當1
# 求date物件是( 哪一年,哪一週,星期幾 )
>>> d.isocalendar()
(2008, 1, 2)
>>> d.isoformat()
'2008-01-01'
# today也是date物件
>>> today = date.today()
# 今天是2007年12月12日星期三
>>> today.year, today.month , today.day, today.isoweekday()
(2007, 12, 12, 3)
展開繼續閱讀........
看完了,收起來,改看下一篇!!!
python除了string內建的功能之外,還有其他玩法,更重要的是配合其他內建的資料結構,像是list, tuple, dictionary進而產生強大的功能。以下就是一點點示範。
字串連結和複製 ('+', '*' )
>>> print 'a' + 'b' + 'c'
abc
>>> print 'abc' * 3
abcabcabc
字串插入 ( tuple ,dictionary )
用tuple插入:
>>> print '%s %s %s' %( 'a' , 'b' , 'c' )
a b c
也可以把template存成字串,等待被插入:
>>> template = '%s %s %s'
>>> print template % ( 'a', 'b' , 'c' )
a b c
用字典插入:
>>> temp_dict = '%(first)s %(second)s %(third)s'
>>> intp_dict = { 'first' : 'how' ,
'second' : 'are',
'third' : 'you',
}
>>> print temp_dict % intp_dict
how are you
字串分解和結合 ( list )
用space來分割字串:
>>> s1 = 'how are you'
>>> s1.split()
['how', 'are', 'you']
>>> a = s1.split()
>>> print a
['how', 'are', 'you']
用減號來組合字串:
>>> '-'.join(a)
'how-are-you'
再示範一次
依照換行符號來分割:
>>> s1 = '''first line
second line
end line'''
>>> a = s1.split( '\n' )
>>> print a
['first line', 'second line', 'end line']
用逗號組合回去 :
>>> s_comma = ','.join(a)
>>> print s_comma
first line,second line,end line
用逗號分割:
>>> s_comma_a = s_comma.split(',')
>>> print s_comma_a
['first line', 'second line', 'end line']
用換行符號組合回去 :
>>> print '\n'.join( s_comma_a )
first line
second line
end line
展開繼續閱讀........
看完了,收起來,改看下一篇!!!