import re
創(chuàng)新互聯(lián)公司于2013年開始,先為揭陽等服務(wù)建站,揭陽等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為揭陽企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。
def command_add(date, event_details, calendar):
'''
Add event_details to the list at calendar[date]
Create date if it was not there
:param date: A string date formatted as "YYYY-MM-DD"
:param event_details: A string describing the event
:param calendars: The calendars database
:return: a string indicating any errors, "" for no errors
'''
try:
p = re.compile(r"\d{4}-\d{2}-\d{2}")
assert p.match(date), "Param date must match YYYY-MM-DD"
assert isinstance(event_details, str), \
"Param event_details must be a string"
if date in calendar:
calendar[date].append(str(event_details))
else:
calendar.update({date: str(event_details)})
except Exception,e:
return str(e)
def main():
calendar = {}
command_add("2015-10-20", "Python class", calendar)
print calendar
command_add("2015-11-01", "go out with friends after test",
calendar)
print calendar
if __name__ == "__main__":
main()
1.
定義一個get_constellation(month,date)函數(shù),來獲取出生日期。
2.
創(chuàng)建一個dates和constellations分別來儲存對應(yīng)的日和星座。
3.
用if語句判斷輸入的日數(shù)是否小于出生月份減一所對應(yīng)的日數(shù)。
4.
如果是就返回月份減一所對應(yīng)的星座,不是就返回出生月份所對應(yīng)的星座。
python格式化日期的方法:可以利用time.asctime(time.local(time.time()))函數(shù)來格式化日期。如果我們要獲取當(dāng)前日期,可以利用時間函數(shù)time()來獲取。
python中要把字符串轉(zhuǎn)換成日期格式需要使用time模塊中的strptime函數(shù),例子如下:
import time
t = time.strptime('2016-05-09 21:09:30', '%y-%m-%d %h:%m:%s')
print(t)執(zhí)行結(jié)果如下:
time.struct_time(tm_year=2016,
tm_mon=5,
tm_mday=9,
tm_hour=21,
tm_min=9,
tm_sec=30,
tm_wday=0,
tm_yday=130,
tm_isdst=-1)
函數(shù)說明:
第一個參數(shù)是要轉(zhuǎn)換成日期格式的字符串,第二個參數(shù)是字符串的格式
函數(shù)官方文檔如下:
help on built-in function strptime in module time:
strptime(...)
strptime(string, format) - struct_time
parse a string to a time tuple according to a format specification.
see the library reference manual for formatting codes (same as
strftime()).
commonly used format codes:
%y year with century as a decimal number.
%m month as a decimal number [01,12].
%d day of the month as a decimal number [01,31].
%h hour (24-hour clock) as a decimal number [00,23].
%m minute as a decimal number [00,59].
%s second as a decimal number [00,61].
%z time zone offset from utc.
%a locale's abbreviated weekday name.
%a locale's full weekday name.
%b locale's abbreviated month name.
%b locale's full month name.
%c locale's appropriate date and time representation.
%i hour (12-hour clock) as a decimal number [01,12].
%p locale's equivalent of either am or pm.
other codes may be available on your platform. see documentation for the c library strftime function.