python计算日期之间的放假日期

yipeiwu_com6年前Python基础

本文实例为大家分享了python计算日期之间的放假日期,供大家参考,具体内容如下

代码如下:

#encoding=utf-8 
 
print '中国' 
 
#自动查询节日 给定起始日期和结束日期,自动获取总共的节假日天数 
 
import datetime 
from dateutil import rrule,easter 
 
 
try: set 
except NameError: from sets import Set as set 
 
#复活节 
def all_easter(start, end): 
  easters = [easter.easter(y) for y in xrange(start.year,end.year+1)] 
  return [d for d in easters if start<=d<=end] 
 
#开始到结束的节礼日列表 
def all_boxing(start, end): 
  one_day = datetime.timedelta(days=1) 
  boxings = [easter.easter(y) + one_day for y in xrange(start.year,end.year+1)] 
  return [d for d in boxings if start<=d<=end]  
 
#返回开始和结束日期之间的圣诞节列表 
def all_christmas(start, end): 
  christmases = [datetime.date(y,12,25) for y in xrange(start.year, end.year + 1)] 
  return [d for d in christmases if start<=d<=end] 
 
#返回劳动节列表 
def all_labor(start, end): 
  labors = rrule.rrule(rrule.YEARLY, bymonth=9, byweekday=rrule.MO(1),dtstart=start, until=end) 
  return [d.date() for d in labors] 
 
#读取设定的节假日 
def read_holidays(start, end, holidays_file='holidays.txt'): 
  try: 
    holidays_file = open(holidays_file) 
  except IOError,err: 
    print 'open failed' 
    return [] 
  holidays = [] 
   
  for line in holidays_file: 
    if line.isspace() or line.startswith('#'): 
      continue 
    try: 
      y,m,d = [int(x.strip()) for x in line.split(',')] 
      date = datetime.date(y,m,d) 
    except ValueError: 
      print 'Invalid line find' 
      continue 
    if start <= date <=end: 
      holidays.append(date)       
  holidays_file.close() 
  return holidays 
   
     
holidays_by_country = { 
'US':(all_easter,all_christmas,all_labor), 
'IT':(all_easter,all_boxing,all_labor) 
} 
 
def holidays(cc,start,end,holidays_file='holidays.txt'): 
  all_holidays= read_holidays(start,end,holidays_file) 
  functions = holidays_by_country.get(cc,()) 
  for function in functions: 
    all_holidays += function(start,end) 
  all_holidays = list(set(all_holidays)) 
  return (len(all_holidays),all_holidays) 
 
test_file = open(r'D:\123.txt','w') 
test_file.write('2014,3,23') 
test_file.close() 
 
print holidays('US',datetime.date(2014,1,1),datetime.date(2014,12,31),r'D:\123.txt') 

打印结果如下:

中国
(4, [datetime.date(2014, 4, 20), datetime.date(2014, 12, 25), datetime.date(2014, 3, 23), datetime.date(2014, 9, 1)])

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

从零学python系列之新版本导入httplib模块报ImportError解决方案

之前用Python 2.7版本的httplib做接口测试时,运行代码都是正常的, 最近开始用Python 3.3之后,再去看以前的代码,发现import httplib出现错误:Unre...

python中sort和sorted排序的实例方法

Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列。 1)排序基础 简单的升序排序是非常容易的。只需要...

在python中使用xlrd获取合并单元格的方法

处理excel表格的时候经常遇到合并单元格的情况,使用xlrd中的merged_cells的方法可以获取当前文档中的所有合并单元格的位置信息。 import xlrd xls =...

Python操作excel的方法总结(xlrd、xlwt、openpyxl)

前言 在处理excel数据时发现了xlwt的局限性–不能写入超过65535行、256列的数据(因为它只支持Excel 2003及之前的版本,在这些版本的Excel中行数和列数有此限制),...

python spyder中读取txt为图片的方法

有时候需要将一个环境中的图片可视化,但是可能这个环境下不方便,因此需要将这个环境下的图像数据保存下来,然后在另一个环境下查看,比如,有一个图像数据,image.txt,里面的数据是图像的...