最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python生成日歷實(shí)例解析

 更新時(shí)間:2014年08月21日 11:23:59   投稿:shichen2014  
這篇文章主要介紹了python生成日歷的方法,實(shí)用了python自帶的 calendar模塊加以實(shí)現(xiàn),需要的朋友可以參考下

本文實(shí)例展示了Python生成日歷的實(shí)現(xiàn)方法。該實(shí)例可實(shí)現(xiàn)一個(gè)月的日歷生成5x7的列表,列表里的沒個(gè)日期為datetime類型,采用python自帶的 calendar 模塊實(shí)現(xiàn)。

程序運(yùn)行結(jié)果如下:

python test.py 2014 09 
2014-08-31 2014-09-01 2014-09-02 2014-09-03 2014-09-04 2014-09-05 2014-09-06 
2014-09-07 2014-09-08 2014-09-09 2014-09-10 2014-09-11 2014-09-12 2014-09-13 
2014-09-14 2014-09-15 2014-09-16 2014-09-17 2014-09-18 2014-09-19 2014-09-20 
2014-09-21 2014-09-22 2014-09-23 2014-09-24 2014-09-25 2014-09-26 2014-09-27 
2014-09-28 2014-09-29 2014-09-30 2014-10-01 2014-10-02 2014-10-03 2014-10-04 

python代碼如下:

#coding:utf-8
# Last modified: 2014-08-21 11:08:08 
import calendar 
import datetime 
import sys 
 
def getcal(y, m): 
 # 從周日開始 
 cal = calendar.Calendar(6) 
 if not isinstance(y, int): y = int(y) 
 if not isinstance(m, int): m = int(m) 
 if m == 1: # 1月份 
  py = y - 1; pm = 12; 
  ny = y; nm = 2 
 elif m == 12: # 12月份 
  py = y; pm = 11 
  ny = y + 1; nm = 1 
 else: 
  py = y; pm = m - 1 
  ny = y; nm = m + 1 
 pcal = cal.monthdayscalendar(py, pm) # 上一月 
 ncal = cal.monthdayscalendar(ny, nm) # 下一月 
 ccal = cal.monthdayscalendar(y, m)  # 當(dāng)前 
 w1 = ccal.pop(0) # 取第一周 
 w2 = ccal.pop() # 取最后一周 
 wp = pcal.pop() # 上個(gè)月的最后一周 
 wn = ncal.pop(0) # 下個(gè)月的第一周 
 #r1 = [datetime.date(y, m ,w1[i]) or wp[i] for i in range(7)] 
 r1 = [w1[i] and datetime.date(y, m, w1[i]) or datetime.date(py, pm, wp[i]) for i in range(7)] 
 r2 = [w2[i] and datetime.date(y, m, w2[i]) or datetime.date(ny, nm, wn[i]) for i in range(7)] 
 # 轉(zhuǎn)datetime 
 result = [] 
 result.append(r1) # 第一周 
 for c in ccal:  # 其他周 
  result.append([datetime.date(y,m,i) for i in c]) 
 result.append(r2) # 最后一周 
 return result 
 
if __name__ == '__main__': 
 for w in getcal(sys.argv[1], sys.argv[2]): 
  for d in w: 
   print d, 
  print 

希望本文所述實(shí)例對(duì)大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論

集安市| 宣化县| 苍溪县| 繁昌县| 咸阳市| 越西县| 勐海县| 土默特右旗| 凯里市| 民乐县| 体育| 额敏县| 昌都县| 新安县| 自治县| 塘沽区| 南溪县| 普兰店市| 成武县| 汪清县| 广宗县| 广河县| 揭西县| 济阳县| 任丘市| 湛江市| 喀喇沁旗| 汶川县| 宽城| 革吉县| 晋中市| 樟树市| 娄烦县| 洪泽县| 思茅市| 大洼县| 开平市| 绥德县| 郁南县| 禄丰县| 陇川县|