python生成日历实例解析


Posted in Python onAugust 21, 2014

本文实例展示了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程序设计有所帮助。

Python 相关文章推荐
python使用xlrd实现检索excel中某列含有指定字符串记录的方法
May 09 Python
Python中的os.path路径模块中的操作方法总结
Jul 07 Python
Unicode和Python的中文处理
Mar 19 Python
Jupyter notebook远程访问服务器的方法
May 24 Python
解决pip install xxx报错SyntaxError: invalid syntax的问题
Nov 30 Python
Python Django中间件,中间件函数,全局异常处理操作示例
Nov 08 Python
python数据类型可变不可变知识点总结
Mar 06 Python
利用pandas向一个csv文件追加写入数据的实现示例
Apr 23 Python
Python openpyxl模块实现excel读写操作
Jun 30 Python
通过代码实例了解Python异常本质
Sep 16 Python
Python scrapy爬取起点中文网小说榜单
Jun 13 Python
python自动获取微信公众号最新文章的实现代码
Jul 15 Python
MySQLdb ImportError: libmysqlclient.so.18解决方法
Aug 21 #Python
Python中除法使用的注意事项
Aug 21 #Python
Python中dictionary items()系列函数的用法实例
Aug 21 #Python
Python 不同对象比较大小示例探讨
Aug 21 #Python
python字符串连接方式汇总
Aug 21 #Python
零基础学Python(一)Python环境安装
Aug 20 #Python
Python中replace方法实例分析
Aug 20 #Python
You might like
生成随机字符串和验证码的类的PHP实例
2013/12/24 PHP
ThinkPHP中的系统常量和预定义常量集合
2014/07/01 PHP
ThinkPHP中ajax使用实例教程
2014/08/22 PHP
PHP Reflection API详解
2015/05/12 PHP
php使用Jpgraph绘制3D饼状图的方法
2015/06/10 PHP
php冒泡排序与快速排序实例详解
2015/12/07 PHP
JavaScript脚本性能的优化方法
2007/02/02 Javascript
extJs 下拉框联动实现代码
2010/04/09 Javascript
js实现鼠标点击左上角滑动菜单效果代码
2015/09/06 Javascript
JavaScript在网页中画圆的函数arc使用方法
2015/11/13 Javascript
JS获取文件大小方法小结
2015/12/08 Javascript
AngularJS实现表单手动验证和表单自动验证
2015/12/09 Javascript
node.js微信公众平台开发教程
2016/03/04 Javascript
canvas 画布在主流浏览器中的尺寸限制详细介绍
2016/12/15 Javascript
微信小程序自定义toast实现方法详解【附demo源码下载】
2017/11/28 Javascript
JS实现简单获取最近7天和最近3天日期的方法
2018/04/18 Javascript
Vue表单demo v-model双向绑定问题
2018/06/29 Javascript
vue 权限认证token的实现方法
2018/07/17 Javascript
微信小程序实现锚点功能
2019/11/20 Javascript
[02:49]DOTA2完美大师赛首日观众采访
2017/11/23 DOTA
[01:11:11]Alliance vs RNG 2019国际邀请赛淘汰赛 败者组BO1 8.20.mp4
2020/07/19 DOTA
python实现的一个火车票转让信息采集器
2014/07/09 Python
Python使用正则表达式抓取网页图片的方法示例
2017/04/21 Python
python编程羊车门问题代码示例
2017/10/25 Python
python实现简单tftp(基于udp协议)
2018/07/30 Python
python看某个模块的版本方法
2018/10/16 Python
django 取消csrf限制的实例
2020/03/13 Python
python中有函数重载吗
2020/05/28 Python
Django修改app名称和数据表迁移方案实现
2020/09/17 Python
html5 video全屏播放/自动播放的实现示例
2020/08/06 HTML / CSS
美国高端医师级美容产品电商:BeautifiedYou.com
2017/04/17 全球购物
雅高酒店中国:Accorhotels.com China
2018/03/26 全球购物
集世界奢侈品和设计师品牌的意大利精品买手店:Tessabit
2019/08/17 全球购物
学术诚信承诺书
2014/05/26 职场文书
国庆65周年演讲稿:回首往昔,展望未来
2014/09/21 职场文书
2015年法务工作总结范文
2015/05/23 职场文书