How to print calendar in monthwise matrix format in Python 3.6?

别等时光非礼了梦想. 提交于 2019-12-08 00:58:26

问题


I have created the Indian National calendar function in Python as below,

import calendar
import datetime

'Days in Tamil names. first day start from 0-Monday and 6- sunday. similar to ISO format'
tamil_day = ('திங்கள்','செவ்வாய்','புதன்','வியாழன்','வெள்ளி','சனி','ஞாயிறு')

'months in Tamil names. first month start from 0-Chithirai and 11-panguni. similar to ISO format'
tamil_months = ('சித்திரை','வைகாசி','ஆணி','ஆடி','ஆவணி','புரட்டாசி','ஐப்பசி','கார்த்திகை','மார்கழி','தை','மாசி','பங்குனி')
'''reference date for Indian Saka Era
Saka Era 1876 is equal to 1954-55AD or Saka 1875-76 is equal to 1954
March 22,1954 is Chithirai 1,1876 Saka as Indian new year
'''
saka_year_AD_reference = datetime.date(1954,3,22)
saka_year_reference = 1876
saka_year_offset = 78
tamil_first_day_reference = calendar.firstweekday()
tamil_first_month_reference = tamil_months[0]
'''
number of days in each tamil month
normal year- chithirai-30days;
leap year- chithirai-31 days
'''
days_in_month_normal_year = (30,31,31,31,31,31,30,30,30,30,30,30)
days_in_month_leap_year = (31,31,31,31,31,31,30,30,30,30,30,30)

'Enter the year to print the calendar'
year = 1876

'find the year is leap year or not'
def tamil_leap_year(year):
    leap=year+saka_year_offset
    return calendar.isleap(leap)

'find the first day in given year'
def tamil_first_day_in_year(year):
    offset_compensation = year+saka_year_offset

    if tamil_leap_year(year):
        return calendar.weekday(offset_compensation,3,21)
    else:
        return calendar.weekday(offset_compensation,3,22)

def print_year(year):
    if tamil_leap_year(year)!=True:
        day_count = tamil_first_day_in_year(year)

        for i in range(12):
            print('\n','{:*^16}'.format(tamil_months[i]),'  \n','_______________')

            for i in range(1,(1+days_in_month_normal_year[i])):
                print('{:<2}'.format(i),"|",'{: <2}'.format(tamil_day[day_count]),'\n',end='')
                day_count = day_count+1

                if i<=31:
                    if day_count==7:
                        day_count = 0
                    continue
                else:
                    break
    else:
        day_count = tamil_first_day_in_year(year)

        for i in range(12):
            print(tamil_months[i],'|')

            for i in range(1,(1+days_in_month_leap_year[i])):
                print(i,"|",tamil_day[day_count])
                day_count = day_count+1

                if i<=31:
                    if day_count==7:
                        day_count = 0
                    continue
                else:
                    break

print(print_year(year))

I want to print the output in month wise and as similar to Gregorian calendar similar as below

      January                   February                   March
 Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
 1  2  3  4  5  6  7                1  2  3  4                1  2  3  4
 8  9 10 11 12 13 14       5  6  7  8  9 10 11       5  6  7  8  9 10 11
 15 16 17 18 19 20 21      12 13 14 15 16 17 18      12 13 14 15 16 17 18
 22 23 24 25 26 27 28      19 20 21 22 23 24 25      19 20 21 22 23 24 25
 29 30 31                  26 27 28                  26 27 28 29 30 31

Unfortunately, I am a beginner to Python and I don't know how to make a loop in order to create frame the month table. Could anyone help me to solve this problem?


回答1:


first convert the integer into string and then add positional function as center(). the problem get solved. thank you



来源:https://stackoverflow.com/questions/51354543/how-to-print-calendar-in-monthwise-matrix-format-in-python-3-6

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!