如何在 Python 3.6 中以按月矩阵格式打印日历?
How to print calendar in monthwise matrix format in Python 3.6?
我在 Python 中创建了印度国家日历功能,如下所示,
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))
我想按月打印输出,类似于公历,如下所示
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
不幸的是,我是 Python 的初学者,我不知道如何制作循环来创建月份 table 的框架。谁能帮我解决这个问题?
首先将整数转换成字符串,然后添加位置函数如center()。问题得到解决。
谢谢
我在 Python 中创建了印度国家日历功能,如下所示,
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))
我想按月打印输出,类似于公历,如下所示
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
不幸的是,我是 Python 的初学者,我不知道如何制作循环来创建月份 table 的框架。谁能帮我解决这个问题?
首先将整数转换成字符串,然后添加位置函数如center()。问题得到解决。 谢谢