国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python生成日歷實例解析

2020-02-23 05:40:20
字體:
來源:轉載
供稿:網友

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

程序運行結果如下:

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)  # 當前  w1 = ccal.pop(0) # 取第一周  w2 = ccal.pop() # 取最后一周  wp = pcal.pop() # 上個月的最后一周  wn = ncal.pop(0) # 下個月的第一周  #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)]  # 轉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 

希望本文所述實例對大家的Python程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 潜山县| 香河县| 谷城县| 汝南县| 固原市| 伊宁县| 衢州市| 迭部县| 金寨县| 凭祥市| 邛崃市| 团风县| 延川县| 宣威市| 龙岩市| 泽州县| 中卫市| 鸡西市| 湖州市| 桐梓县| 万全县| 阆中市| 搜索| 莱西市| 墨玉县| 泰顺县| 德庆县| 福泉市| 屯留县| 锡林浩特市| 广昌县| 抚州市| 河西区| 黑水县| 平乐县| 香河县| 屯留县| 岳普湖县| 阜阳市| 海阳市| 瓦房店市|