Python generates calendar instance resolution, python calendar instance
This example shows how to generate a calendar in Python. This instance can generate a 5x7 list for a calendar month. No date in the list is of the datetime type and is implemented using the calendar module provided by python.
The program running result is as follows:
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
The python code is as follows:
# Coding: UTF-8 # Last modified: 11:08:08 import calendar import datetime import sys def getcal (y, m): # from Sunday onwards cal = calendar. calendar (6) if not isinstance (y, int): y = int (y) if not isinstance (m, int): m = int (m) if m = 1: # py = y-1 in March; pm = 12; ny = y; nm = 2 elif m = 12: # py = y in March; 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 next month. monthdayscalendar (y, m) # current w1 = ccal. pop (0) # obtain the first week w2 = ccal. pop () # obtain wp = pcal for the last week. pop () # wn = ncal in the last week of last month. pop (0) # The first week of the next month # 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)] # convert datetime result = [] result. append (r1) # week 1 for c in ccal: # result of other weeks. append ([datetime. date (y, m, I) for I in c]) result. append (r2) # return result if _ name _ = '_ main _' in the last week: for w in getcal (sys. argv [1], sys. argv [2]): for d in w: print d, print
I hope the examples described in this article will help you with Python program design.
In python, how does one output the calendar in the following format?
Import calendar
C = calendar. TextCalendar (6)
Print c. prmonth (2011, 11)
How to Write a calendar query program in python without referencing the module?
#! /Usr/bin/evn python
Def simpleCalendar ():
DaysOfMonth = daysOfMonths = daysOfYears = month = year = 0
Yearfun = lambda year: year % 400 = 0 or year % 4 = 0 and year % 100! = 0
Print "*" * 30, "calendar", "*" * 30
Year = input ("enter year :")
Leap = yearfun (year)
Month = input ('enter month :')
Assert month in range (1, 13), "month must in 1-12"
If month = 2:
DaysOfMonth = 29 if leap else 28
Else:
DaysOfMonth = 30 if month in [4, 6, 9, 11] else 31
For I in range (1, month ):
If I = 2:
DaysOfMonths + = 29 if leap else 28
Else:
DaysOfMonths + = 30 if I in [4, 6, 9, 11] else 31
For I in range (1900, year ):
DaysOfYears + = 366 if yearfun (I) else 365
DaysOfYears + = daysOfMonths
Weak = 1 + daysOfYears % 7
Print "Sun \ tMon \ tTue \ tWed \ tThu \ tFri \ tSat"
Print "\ t" * weak,
For I in range (1, daysOfMonth + 1 ):
Weak + = 1
If weak % 7! = 0:
Print I, '\ t ',
Else:
Print I, '\ n ',
If _ name __= = '_ main __':
SimpleCalendar ()