python生成指定年份所有的天,并计算每天属于一年的第几周和周几
今天有一个功能点是需要按照日历上面的日期来进行一些数据的计算工作,在计算之前我需要将每一天属于一年52周里面的第几周和周几计算出来,在计算之前我还需要有一年内的所有天的日期,整体的思路是十分清晰的,实现流程也没有特别复杂的地方,主要就是要细致一点,好了,不多说了,具体实现如下:
#!usr/bin/env python
# encoding:utf-8
from __future__ import division"""
__Author__:沂水寒城
功能: python生成指定年份所有的天,并计算每天属于一年的第几周和周几
"""import time
import datetimemonth_list = ["01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"]
month_day_dict = {"01": 31,"02": 28,"03": 31,"04": 30,"05": 31,"06": 30,"07": 31,"08": 31,"09": 30,"10": 31,"11": 30,"12": 31,
}def string2Datetime(timestamp, format="%Y-%m-%d %H:%M:%S"):"""把字符串转成datetime"""return datetime.datetime.strptime(timestamp, format)def judgeRunYear(year=2019):"""判断是否是闰年"""if (year % 100 != 0 and year % 4 == 0) or (year % 100 == 0 and year % 400 == 0):return Trueelse:return Falsedef generateMonthDays(month_day_dict, year="2017", month="03"):"""生成指定年份、月份中的所有日期"""day_num = month_day_dict[month]day_date_list = []for i in range(1, day_num + 1):one = str(i)if len(one) == 1:one = "0" + oneday_date_list.append(year + "-" + month + "-" + one)return day_date_listdef genenrateYearDays(year=2019):"""生成一年中所有的日期"""res_list = []if judgeRunYear(year=year):month_day_dict["02"] = 29for one_mon in month_list:one_mon_day = generateMonthDays(month_day_dict, year=str(year), month=one_mon)res_list += one_mon_dayreturn res_listdef singleDateHandle(day="2019-07-19", format="%Y-%m-%d"):"""单日处理"""T = string2Datetime(day, format=format)weekDay = T.weekday() + 1weekNum = T.isocalendar()[1]return weekDay, weekNumdef genenrateYearDaysWeek(year=2019):"""计算生成一年中每一天属于 第几周 周几"""year_day_list = genenrateYearDays(year=year)for one_day in year_day_list:weekDay, weekNum = singleDateHandle(day=one_day)print("{0} is: {1}th week, {2}th day.".format(one_day, weekNum, weekDay))if __name__ == "__main__":genenrateYearDaysWeek(year=2019)
我们以2019年为例,看一下计算结果,输出如下:
2019-01-01 is: 1th week, 2th day.
2019-01-02 is: 1th week, 3th day.
2019-01-03 is: 1th week, 4th day.
2019-01-04 is: 1th week, 5th day.
2019-01-05 is: 1th week, 6th day.
2019-01-06 is: 1th week, 7th day.
2019-01-07 is: 2th week, 1th day.
2019-01-08 is: 2th week, 2th day.
2019-01-09 is: 2th week, 3th day.
2019-01-10 is: 2th week, 4th day.
2019-01-11 is: 2th week, 5th day.
2019-01-12 is: 2th week, 6th day.
2019-01-13 is: 2th week, 7th day.
2019-01-14 is: 3th week, 1th day.
2019-01-15 is: 3th week, 2th day.
2019-01-16 is: 3th week, 3th day.
2019-01-17 is: 3th week, 4th day.
2019-01-18 is: 3th week, 5th day.
2019-01-19 is: 3th week, 6th day.
2019-01-20 is: 3th week, 7th day.
2019-01-21 is: 4th week, 1th day.
2019-01-22 is: 4th week, 2th day.
2019-01-23 is: 4th week, 3th day.
2019-01-24 is: 4th week, 4th day.
2019-01-25 is: 4th week, 5th day.
2019-01-26 is: 4th week, 6th day.
2019-01-27 is: 4th week, 7th day.
2019-01-28 is: 5th week, 1th day.
2019-01-29 is: 5th week, 2th day.
2019-01-30 is: 5th week, 3th day.
2019-01-31 is: 5th week, 4th day.
2019-02-01 is: 5th week, 5th day.
2019-02-02 is: 5th week, 6th day.
2019-02-03 is: 5th week, 7th day.
2019-02-04 is: 6th week, 1th day.
2019-02-05 is: 6th week, 2th day.
2019-02-06 is: 6th week, 3th day.
2019-02-07 is: 6th week, 4th day.
2019-02-08 is: 6th week, 5th day.
2019-02-09 is: 6th week, 6th day.
2019-02-10 is: 6th week, 7th day.
2019-02-11 is: 7th week, 1th day.
2019-02-12 is: 7th week, 2th day.
2019-02-13 is: 7th week, 3th day.
2019-02-14 is: 7th week, 4th day.
2019-02-15 is: 7th week, 5th day.
2019-02-16 is: 7th week, 6th day.
2019-02-17 is: 7th week, 7th day.
2019-02-18 is: 8th week, 1th day.
2019-02-19 is: 8th week, 2th day.
2019-02-20 is: 8th week, 3th day.
2019-02-21 is: 8th week, 4th day.
2019-02-22 is: 8th week, 5th day.
2019-02-23 is: 8th week, 6th day.
2019-02-24 is: 8th week, 7th day.
2019-02-25 is: 9th week, 1th day.
2019-02-26 is: 9th week, 2th day.
2019-02-27 is: 9th week, 3th day.
2019-02-28 is: 9th week, 4th day.
2019-03-01 is: 9th week, 5th day.
2019-03-02 is: 9th week, 6th day.
2019-03-03 is: 9th week, 7th day.
2019-03-04 is: 10th week, 1th day.
2019-03-05 is: 10th week, 2th day.
2019-03-06 is: 10th week, 3th day.
2019-03-07 is: 10th week, 4th day.
2019-03-08 is: 10th week, 5th day.
2019-03-09 is: 10th week, 6th day.
2019-03-10 is: 10th week, 7th day.
2019-03-11 is: 11th week, 1th day.
2019-03-12 is: 11th week, 2th day.
2019-03-13 is: 11th week, 3th day.
2019-03-14 is: 11th week, 4th day.
2019-03-15 is: 11th week, 5th day.
2019-03-16 is: 11th week, 6th day.
2019-03-17 is: 11th week, 7th day.
2019-03-18 is: 12th week, 1th day.
2019-03-19 is: 12th week, 2th day.
2019-03-20 is: 12th week, 3th day.
2019-03-21 is: 12th week, 4th day.
2019-03-22 is: 12th week, 5th day.
2019-03-23 is: 12th week, 6th day.
2019-03-24 is: 12th week, 7th day.
2019-03-25 is: 13th week, 1th day.
2019-03-26 is: 13th week, 2th day.
2019-03-27 is: 13th week, 3th day.
2019-03-28 is: 13th week, 4th day.
2019-03-29 is: 13th week, 5th day.
2019-03-30 is: 13th week, 6th day.
2019-03-31 is: 13th week, 7th day.
2019-04-01 is: 14th week, 1th day.
2019-04-02 is: 14th week, 2th day.
2019-04-03 is: 14th week, 3th day.
2019-04-04 is: 14th week, 4th day.
2019-04-05 is: 14th week, 5th day.
2019-04-06 is: 14th week, 6th day.
2019-04-07 is: 14th week, 7th day.
2019-04-08 is: 15th week, 1th day.
2019-04-09 is: 15th week, 2th day.
2019-04-10 is: 15th week, 3th day.
2019-04-11 is: 15th week, 4th day.
2019-04-12 is: 15th week, 5th day.
2019-04-13 is: 15th week, 6th day.
2019-04-14 is: 15th week, 7th day.
2019-04-15 is: 16th week, 1th day.
2019-04-16 is: 16th week, 2th day.
2019-04-17 is: 16th week, 3th day.
2019-04-18 is: 16th week, 4th day.
2019-04-19 is: 16th week, 5th day.
2019-04-20 is: 16th week, 6th day.
2019-04-21 is: 16th week, 7th day.
2019-04-22 is: 17th week, 1th day.
2019-04-23 is: 17th week, 2th day.
2019-04-24 is: 17th week, 3th day.
2019-04-25 is: 17th week, 4th day.
2019-04-26 is: 17th week, 5th day.
2019-04-27 is: 17th week, 6th day.
2019-04-28 is: 17th week, 7th day.
2019-04-29 is: 18th week, 1th day.
2019-04-30 is: 18th week, 2th day.
2019-05-01 is: 18th week, 3th day.
2019-05-02 is: 18th week, 4th day.
2019-05-03 is: 18th week, 5th day.
2019-05-04 is: 18th week, 6th day.
2019-05-05 is: 18th week, 7th day.
2019-05-06 is: 19th week, 1th day.
2019-05-07 is: 19th week, 2th day.
2019-05-08 is: 19th week, 3th day.
2019-05-09 is: 19th week, 4th day.
2019-05-10 is: 19th week, 5th day.
2019-05-11 is: 19th week, 6th day.
2019-05-12 is: 19th week, 7th day.
2019-05-13 is: 20th week, 1th day.
2019-05-14 is: 20th week, 2th day.
2019-05-15 is: 20th week, 3th day.
2019-05-16 is: 20th week, 4th day.
2019-05-17 is: 20th week, 5th day.
2019-05-18 is: 20th week, 6th day.
2019-05-19 is: 20th week, 7th day.
2019-05-20 is: 21th week, 1th day.
2019-05-21 is: 21th week, 2th day.
2019-05-22 is: 21th week, 3th day.
2019-05-23 is: 21th week, 4th day.
2019-05-24 is: 21th week, 5th day.
2019-05-25 is: 21th week, 6th day.
2019-05-26 is: 21th week, 7th day.
2019-05-27 is: 22th week, 1th day.
2019-05-28 is: 22th week, 2th day.
2019-05-29 is: 22th week, 3th day.
2019-05-30 is: 22th week, 4th day.
2019-05-31 is: 22th week, 5th day.
2019-06-01 is: 22th week, 6th day.
2019-06-02 is: 22th week, 7th day.
2019-06-03 is: 23th week, 1th day.
2019-06-04 is: 23th week, 2th day.
2019-06-05 is: 23th week, 3th day.
2019-06-06 is: 23th week, 4th day.
2019-06-07 is: 23th week, 5th day.
2019-06-08 is: 23th week, 6th day.
2019-06-09 is: 23th week, 7th day.
2019-06-10 is: 24th week, 1th day.
2019-06-11 is: 24th week, 2th day.
2019-06-12 is: 24th week, 3th day.
2019-06-13 is: 24th week, 4th day.
2019-06-14 is: 24th week, 5th day.
2019-06-15 is: 24th week, 6th day.
2019-06-16 is: 24th week, 7th day.
2019-06-17 is: 25th week, 1th day.
2019-06-18 is: 25th week, 2th day.
2019-06-19 is: 25th week, 3th day.
2019-06-20 is: 25th week, 4th day.
2019-06-21 is: 25th week, 5th day.
2019-06-22 is: 25th week, 6th day.
2019-06-23 is: 25th week, 7th day.
2019-06-24 is: 26th week, 1th day.
2019-06-25 is: 26th week, 2th day.
2019-06-26 is: 26th week, 3th day.
2019-06-27 is: 26th week, 4th day.
2019-06-28 is: 26th week, 5th day.
2019-06-29 is: 26th week, 6th day.
2019-06-30 is: 26th week, 7th day.
2019-07-01 is: 27th week, 1th day.
2019-07-02 is: 27th week, 2th day.
2019-07-03 is: 27th week, 3th day.
2019-07-04 is: 27th week, 4th day.
2019-07-05 is: 27th week, 5th day.
2019-07-06 is: 27th week, 6th day.
2019-07-07 is: 27th week, 7th day.
2019-07-08 is: 28th week, 1th day.
2019-07-09 is: 28th week, 2th day.
2019-07-10 is: 28th week, 3th day.
2019-07-11 is: 28th week, 4th day.
2019-07-12 is: 28th week, 5th day.
2019-07-13 is: 28th week, 6th day.
2019-07-14 is: 28th week, 7th day.
2019-07-15 is: 29th week, 1th day.
2019-07-16 is: 29th week, 2th day.
2019-07-17 is: 29th week, 3th day.
2019-07-18 is: 29th week, 4th day.
2019-07-19 is: 29th week, 5th day.
2019-07-20 is: 29th week, 6th day.
2019-07-21 is: 29th week, 7th day.
2019-07-22 is: 30th week, 1th day.
2019-07-23 is: 30th week, 2th day.
2019-07-24 is: 30th week, 3th day.
2019-07-25 is: 30th week, 4th day.
2019-07-26 is: 30th week, 5th day.
2019-07-27 is: 30th week, 6th day.
2019-07-28 is: 30th week, 7th day.
2019-07-29 is: 31th week, 1th day.
2019-07-30 is: 31th week, 2th day.
2019-07-31 is: 31th week, 3th day.
2019-08-01 is: 31th week, 4th day.
2019-08-02 is: 31th week, 5th day.
2019-08-03 is: 31th week, 6th day.
2019-08-04 is: 31th week, 7th day.
2019-08-05 is: 32th week, 1th day.
2019-08-06 is: 32th week, 2th day.
2019-08-07 is: 32th week, 3th day.
2019-08-08 is: 32th week, 4th day.
2019-08-09 is: 32th week, 5th day.
2019-08-10 is: 32th week, 6th day.
2019-08-11 is: 32th week, 7th day.
2019-08-12 is: 33th week, 1th day.
2019-08-13 is: 33th week, 2th day.
2019-08-14 is: 33th week, 3th day.
2019-08-15 is: 33th week, 4th day.
2019-08-16 is: 33th week, 5th day.
2019-08-17 is: 33th week, 6th day.
2019-08-18 is: 33th week, 7th day.
2019-08-19 is: 34th week, 1th day.
2019-08-20 is: 34th week, 2th day.
2019-08-21 is: 34th week, 3th day.
2019-08-22 is: 34th week, 4th day.
2019-08-23 is: 34th week, 5th day.
2019-08-24 is: 34th week, 6th day.
2019-08-25 is: 34th week, 7th day.
2019-08-26 is: 35th week, 1th day.
2019-08-27 is: 35th week, 2th day.
2019-08-28 is: 35th week, 3th day.
2019-08-29 is: 35th week, 4th day.
2019-08-30 is: 35th week, 5th day.
2019-08-31 is: 35th week, 6th day.
2019-09-01 is: 35th week, 7th day.
2019-09-02 is: 36th week, 1th day.
2019-09-03 is: 36th week, 2th day.
2019-09-04 is: 36th week, 3th day.
2019-09-05 is: 36th week, 4th day.
2019-09-06 is: 36th week, 5th day.
2019-09-07 is: 36th week, 6th day.
2019-09-08 is: 36th week, 7th day.
2019-09-09 is: 37th week, 1th day.
2019-09-10 is: 37th week, 2th day.
2019-09-11 is: 37th week, 3th day.
2019-09-12 is: 37th week, 4th day.
2019-09-13 is: 37th week, 5th day.
2019-09-14 is: 37th week, 6th day.
2019-09-15 is: 37th week, 7th day.
2019-09-16 is: 38th week, 1th day.
2019-09-17 is: 38th week, 2th day.
2019-09-18 is: 38th week, 3th day.
2019-09-19 is: 38th week, 4th day.
2019-09-20 is: 38th week, 5th day.
2019-09-21 is: 38th week, 6th day.
2019-09-22 is: 38th week, 7th day.
2019-09-23 is: 39th week, 1th day.
2019-09-24 is: 39th week, 2th day.
2019-09-25 is: 39th week, 3th day.
2019-09-26 is: 39th week, 4th day.
2019-09-27 is: 39th week, 5th day.
2019-09-28 is: 39th week, 6th day.
2019-09-29 is: 39th week, 7th day.
2019-09-30 is: 40th week, 1th day.
2019-10-01 is: 40th week, 2th day.
2019-10-02 is: 40th week, 3th day.
2019-10-03 is: 40th week, 4th day.
2019-10-04 is: 40th week, 5th day.
2019-10-05 is: 40th week, 6th day.
2019-10-06 is: 40th week, 7th day.
2019-10-07 is: 41th week, 1th day.
2019-10-08 is: 41th week, 2th day.
2019-10-09 is: 41th week, 3th day.
2019-10-10 is: 41th week, 4th day.
2019-10-11 is: 41th week, 5th day.
2019-10-12 is: 41th week, 6th day.
2019-10-13 is: 41th week, 7th day.
2019-10-14 is: 42th week, 1th day.
2019-10-15 is: 42th week, 2th day.
2019-10-16 is: 42th week, 3th day.
2019-10-17 is: 42th week, 4th day.
2019-10-18 is: 42th week, 5th day.
2019-10-19 is: 42th week, 6th day.
2019-10-20 is: 42th week, 7th day.
2019-10-21 is: 43th week, 1th day.
2019-10-22 is: 43th week, 2th day.
2019-10-23 is: 43th week, 3th day.
2019-10-24 is: 43th week, 4th day.
2019-10-25 is: 43th week, 5th day.
2019-10-26 is: 43th week, 6th day.
2019-10-27 is: 43th week, 7th day.
2019-10-28 is: 44th week, 1th day.
2019-10-29 is: 44th week, 2th day.
2019-10-30 is: 44th week, 3th day.
2019-10-31 is: 44th week, 4th day.
2019-11-01 is: 44th week, 5th day.
2019-11-02 is: 44th week, 6th day.
2019-11-03 is: 44th week, 7th day.
2019-11-04 is: 45th week, 1th day.
2019-11-05 is: 45th week, 2th day.
2019-11-06 is: 45th week, 3th day.
2019-11-07 is: 45th week, 4th day.
2019-11-08 is: 45th week, 5th day.
2019-11-09 is: 45th week, 6th day.
2019-11-10 is: 45th week, 7th day.
2019-11-11 is: 46th week, 1th day.
2019-11-12 is: 46th week, 2th day.
2019-11-13 is: 46th week, 3th day.
2019-11-14 is: 46th week, 4th day.
2019-11-15 is: 46th week, 5th day.
2019-11-16 is: 46th week, 6th day.
2019-11-17 is: 46th week, 7th day.
2019-11-18 is: 47th week, 1th day.
2019-11-19 is: 47th week, 2th day.
2019-11-20 is: 47th week, 3th day.
2019-11-21 is: 47th week, 4th day.
2019-11-22 is: 47th week, 5th day.
2019-11-23 is: 47th week, 6th day.
2019-11-24 is: 47th week, 7th day.
2019-11-25 is: 48th week, 1th day.
2019-11-26 is: 48th week, 2th day.
2019-11-27 is: 48th week, 3th day.
2019-11-28 is: 48th week, 4th day.
2019-11-29 is: 48th week, 5th day.
2019-11-30 is: 48th week, 6th day.
2019-12-01 is: 48th week, 7th day.
2019-12-02 is: 49th week, 1th day.
2019-12-03 is: 49th week, 2th day.
2019-12-04 is: 49th week, 3th day.
2019-12-05 is: 49th week, 4th day.
2019-12-06 is: 49th week, 5th day.
2019-12-07 is: 49th week, 6th day.
2019-12-08 is: 49th week, 7th day.
2019-12-09 is: 50th week, 1th day.
2019-12-10 is: 50th week, 2th day.
2019-12-11 is: 50th week, 3th day.
2019-12-12 is: 50th week, 4th day.
2019-12-13 is: 50th week, 5th day.
2019-12-14 is: 50th week, 6th day.
2019-12-15 is: 50th week, 7th day.
2019-12-16 is: 51th week, 1th day.
2019-12-17 is: 51th week, 2th day.
2019-12-18 is: 51th week, 3th day.
2019-12-19 is: 51th week, 4th day.
2019-12-20 is: 51th week, 5th day.
2019-12-21 is: 51th week, 6th day.
2019-12-22 is: 51th week, 7th day.
2019-12-23 is: 52th week, 1th day.
2019-12-24 is: 52th week, 2th day.
2019-12-25 is: 52th week, 3th day.
2019-12-26 is: 52th week, 4th day.
2019-12-27 is: 52th week, 5th day.
2019-12-28 is: 52th week, 6th day.
2019-12-29 is: 52th week, 7th day.
2019-12-30 is: 1th week, 1th day.
2019-12-31 is: 1th week, 2th day.
出来了结果怎么核实呢?我们直接看今天的日期就好了,如下所示:
我们分别截取了系统时间和程序输出时间,可以看到周几这个结果是正确的,至于第几周感兴趣的话可以对照系统时间数一数就好了,我验证过了是没有问题。
python生成指定年份所有的天,并计算每天属于一年的第几周和周几相关推荐
- python生成指定位数随机数_python生成指定长度的随机数密码
复制代码 代码如下: #!/usr/bin/env python # -*- coding:utf-8 -*- #导入random和string模块 import random, string def ...
- python 生成指定概率的随机数
生成指定概率的随机数 需求 解决方法 需求 遇到一些情况需要指定概率的条件下生成随机数,比如要生成姓名字符串,随机生成的话一般是两个字的姓名和3个字的姓名概率是一样的,而实际应该是大部分偏向三个字的, ...
- python生成指定长度的列表_如何在python中创建固定大小列表?
(tl;博士:您问题的确切答案是numpy.empty_like或x = list(size=10000),但您可能不在乎,可以使用myList = [None]*10000逃脱.) 简单的方法 您可 ...
- python输出输入的指定位数的密码_用python生成指定位数的密码
#!/usr/bin/env python #coding:utf8 #随机生成8位.20位.10位密码 import random import string all_chs = string.le ...
- excel中怎么生成指定年份的日期
=RANDBETWEEN(DATE($A$79,1,1),DATE($A$79,12,31)) RAND()得到一组随机数 RANK(B2,$B$2:$B$13) 得到每个随机数的排名 公式3利用文本 ...
- python生成指定长度的列表_python怎样创建具有一定长度和初始值的列表
展开全部 python的数据是可以动态2113增长的,直接定义使用5261a=[]即可: 比如:a=[0,1,2],这时a[0]=0, a[1]=1, a[[2]=2:如果4102数组想a想定义165 ...
- python生成指定长度的列表_python – 如何将列表或字符串解析为固定长度的块
有人提出了一个相关的问题: Slicing a list into a list of sub-lists 例如,如果您的源列表是: the_list = [1,2,3,4,5,6,7,8,9,... ...
- 记录一个需求:折线图,要求指定年份每一天的记录
根据自己的业务去修改,本文提供参考思路 首先:需求是要求指定年份每一天的价格,没有则补全并赋值日期之前最近的一次价格 有一个主表(主表中有id和价格) 子表中有主表的id和价格 主表价格更改,就需要向 ...
- 【Python笔记】获取星期几在指定年份的所有日期
目录 问题描述:获取星期几在指定年份的所有日期 1. 大致思路 2. 源码 2.1 获取所有日期函数getAllDayDate() 2.2 获取指定周几的所有日期函数getdaydate() 2.3 ...
- python输出日期语句_python使用calendar输出指定年份全年日历的方法
python使用calendar输出指定年份全年日历的方法 本文实例讲述了python使用calendar输出指定年份全年日历的方法.分享给大家供大家参考.具体实现方法如下: import calen ...
最新文章
- 什么是C ++ 11中的lambda表达式?
- Eclipse工作空间还原到最初状态
- .Net Core应用框架Util介绍(五)
- 李宏毅 课程打包_按功能而不是按层打包课程
- 分包组包 北斗通信_蓝牙mesh底层传输层(分包和组包)
- 虚拟化桌面初始化配置处理
- 它来了,带着曝光图又来了!疑似小米MIX4谍照流出
- Java SQL注入学习笔记
- Eclipse用法和技巧十四:自动生成的TODO注释2
- 五步法建设你的数据中台
- 关于Java中的final关键字
- 关于模板函数声明与定义的问题
- R语言各种假设检验实例整理(常用)
- iPhone OS 4.0发布 苹果手机进入多任务时代
- 爬虫I号 :获取免费代理服务器进行代理验证
- C51中intrins_h头文件解释分析
- Linux mtd与ubi关系详解,ubi使用命令总结
- 小程序碰上浏览器搜索入口!
- U V风和真实风向风速
- 使用FileZilla配置FTP服务器