如何在Python 3.6中以月矩阵格式打印日历?

2024-03-25

我用 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的初学者,我不知道如何创建循环来创建月份表的框架。有人能帮我解决这个问题吗?


首先将整数转换为字符串,然后添加位置函数 center()。问题得到解决。 谢谢

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在Python 3.6中以月矩阵格式打印日历? 的相关文章

  • 没有名为 crypto.cipher 的模块

    我现在正在尝试加密一段时间 我最近得到了这个基于 python 的密码器 名为PythonCrypter https github com jbertman PythonCrypter 我对 Python 相当陌生 当我尝试通过终端打开 C
  • SQLAlchemy 通过关联对象声明式多对多自连接

    我有一个用户表和一个朋友表 它将用户映射到其他用户 因为每个用户可以有很多朋友 这个关系显然是对称的 如果用户A是用户B的朋友 那么用户B也是用户A的朋友 我只存储这个关系一次 除了两个用户 ID 之外 Friends 表还有其他字段 因此
  • OpenCV Python cv2.mixChannels()

    我试图将其从 C 转换为 Python 但它给出了不同的色调结果 In C Transform it to HSV cvtColor src hsv CV BGR2HSV Use only the Hue value hue create
  • Django:按钮链接

    我是一名 Django 新手用户 尝试创建一个按钮 单击该按钮会链接到我网站中的另一个页面 我尝试了一些不同的例子 但似乎没有一个对我有用 举个例子 为什么这不起作用
  • Flask 会话变量

    我正在用 Flask 编写一个小型网络应用程序 当两个用户 在同一网络下 尝试使用应用程序时 我遇到会话变量问题 这是代码 import os from flask import Flask request render template
  • 从字符串中删除识别的日期

    作为输入 我有几个包含不同格式日期的字符串 例如 彼得在16 45 我的生日是1990年7月8日 On 7 月 11 日星期六我会回家 I use dateutil parser parse识别字符串中的日期 在下一步中 我想从字符串中删除
  • 如何替换 pandas 数据框列中的重音符号

    我有一个数据框dataSwiss其中包含瑞士城市的信息 我想用普通字母替换带有重音符号的字母 这就是我正在做的 dataSwiss Municipality dataSwiss Municipality str encode utf 8 d
  • python 相当于 R 中的 get() (= 使用字符串检索符号的值)

    在 R 中 get s 函数检索名称存储在字符变量 向量 中的符号的值s e g X lt 10 r lt XVI s lt substr r 1 1 X get s 10 取罗马数字的第一个符号r并将其转换为其等效整数 尽管花了一些时间翻
  • Python 函数可以从作用域之外赋予新属性吗?

    我不知道你可以这样做 def tom print tom s locals locals def dick z print z name z name z guest Harry print z guest z guest print di
  • 在Python中获取文件描述符的位置

    比如说 我有一个原始数字文件描述符 我需要根据它获取文件中的当前位置 import os psutil some code that works with file lp lib open path to file p psutil Pro
  • Jupyter Notebook 内核一直很忙

    我已经安装了 anaconda 并且 python 在 Spyder IPython 等中工作正常 但是我无法运行 python 笔记本 内核被创建 它也连接 但它始终显示黑圈忙碌符号 防火墙或防病毒软件没有问题 我尝试过禁用两者 我也无法
  • 将图像分割成多个网格

    我使用下面的代码将图像分割成网格的 20 个相等的部分 import cv2 im cv2 imread apple jpg im cv2 resize im 1000 500 imgwidth im shape 0 imgheight i
  • 每个 X 具有多个 Y 值的 Python 散点图

    我正在尝试使用 Python 创建一个散点图 其中包含两个 X 类别 cat1 cat2 每个类别都有多个 Y 值 如果每个 X 值的 Y 值的数量相同 我可以使用以下代码使其工作 import numpy as np import mat
  • 为字典中的一个键附加多个值[重复]

    这个问题在这里已经有答案了 我是 python 新手 我有每年的年份和值列表 我想要做的是检查字典中是否已存在该年份 如果存在 则将该值附加到特定键的值列表中 例如 我有一个年份列表 并且每年都有一个值 2010 2 2009 4 1989
  • 解释 Python 中的数字范围

    在 Pylons Web 应用程序中 我需要获取一个字符串 例如 关于如何做到这一点有什么建议吗 我是 Python 新手 我还没有找到任何可以帮助解决此类问题的东西 该列表将是 1 2 3 45 46 48 49 50 51 77 使用
  • 类型错误:预期单个张量时的张量列表 - 将 const 与 tf.random_normal 一起使用时

    我有以下 TensorFlow 代码 tf constant tf random normal time step batch size 1 1 我正进入 状态TypeError List of Tensors when single Te
  • Conda SafetyError:文件大小不正确

    使用创建 Conda 环境时conda create n env name python 3 6 我收到以下警告 Preparing transaction done Verifying transaction SafetyError Th
  • 如何计算 pandas 数据帧上的连续有序值

    我试图从给定的数据帧中获取连续 0 值的最大计数 其中包含来自 pandas 数据帧的 id date value 列 如下所示 id date value 354 2019 03 01 0 354 2019 03 02 0 354 201
  • 使用 Python 的 matplotlib 选择在屏幕上显示哪些图形以及将哪些图形保存到文件中

    我想用Python创建不同的图形matplotlib pyplot 然后 我想将其中一些保存到文件中 而另一些则应使用show 命令 然而 show 显示all创建的数字 我可以通过调用来避免这种情况close 创建我不想在屏幕上显示的绘图
  • NotImplementedError:无法将符号张量 (lstm_2/strided_slice:0) 转换为 numpy 数组。时间

    张量流版本 2 3 1 numpy 版本 1 20 在代码下面 define model model Sequential model add LSTM 50 activation relu input shape n steps n fe

随机推荐