工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
home  MfgRobots >> 工業製造 >  >> Industrial programming >> Python

例を使用した Python CALENDAR チュートリアル

Python の Calendar モジュールには、日付、月、および年に基づくさまざまなタスクの計算を可能にするカレンダー クラスがあります。それに加えて、Python の TextCalendar および HTMLCalendar クラスを使用すると、カレンダーを編集して、必要に応じて使用できます。

Python Calendar で何ができるか見てみましょう。

ステップ 1) コードを実行します。

値を日曜日から木曜日にすばやく変更して、出力を確認してみましょう

ステップ 2) カレンダーを HTML 形式で印刷することもできます。この機能は、開発者がカレンダーのルック アンド フィールを変更したい場合に役立ちます

ステップ 3) c.itermonthday (2025,4) を使用して月の日数をループし、その月の合計日数を取得します。

ステップ 4) 月や曜日など、ローカル システムからデータを取得できます

ステップ 5) 年間の特定の日のリストを取得できます。たとえば、毎週第 1 月曜日が監査日です。各月の最初の月曜日の日付を知りたいとします。このコードを使用できます

これが完全なコードです

Python 2 の例

import calendar
# Create a plain text calendar
c = calendar.TextCalendar(calendar.THURSDAY)
str = c.formatmonth(2025, 1, 0, 0)
print str

# Create an HTML formatted calendar
hc = calendar.HTMLCalendar(calendar.THURSDAY)
str = hc.formatmonth(2025, 1)
print str
# loop over the days of a month
# zeroes indicate that the day of the week is in a next month or overlapping month
for i in c.itermonthdays(2025, 4):
    print i

    # The calendar can give info based on local such a names of days and months (full and abbreviated forms)
    for name in calendar.month_name:
        print name
    for day in calendar.day_name:
        print day
    # calculate days based on a rule: For instance an audit day on the second Monday of every month
    # Figure out what days that would be for each month, we can use the script as shown here
    for month in range(1, 13):
		# It retrieves a list of weeks that represent the month
        mycal = calendar.monthcalendar(2025, month)
		# The first MONDAY has to be within the first two weeks
        week1 = mycal[0]
        week2 = mycal[1]
        if week1[calendar.MONDAY] != 0:
            auditday = week1[calendar.MONDAY]
        else:
        # if the first MONDAY isn't in the first week, it must be in the second week
        	auditday = week2[calendar.MONDAY]
print "%10s %2d" % (calendar.month_name[month], auditday)

Python 3 の例

import calendar
# Create a plain text calendar
c = calendar.TextCalendar(calendar.THURSDAY)
str = c.formatmonth(2025, 1, 0, 0)
print(str)

# Create an HTML formatted calendar
hc = calendar.HTMLCalendar(calendar.THURSDAY)
str = hc.formatmonth(2025, 1)
print(str)
# loop over the days of a month
# zeroes indicate that the day of the week is in a next month or overlapping month
for i in c.itermonthdays(2025, 4):
    print(i)

    # The calendar can give info based on local such a names of days and months (full and abbreviated forms)
    for name in calendar.month_name:
        print(name)
    for day in calendar.day_name:
        print(day)
    # calculate days based on a rule: For instance an audit day on the second Monday of every month
    # Figure out what days that would be for each month, we can use the script as shown here
    for month in range(1, 13):
		# It retrieves a list of weeks that represent the month
        mycal = calendar.monthcalendar(2025, month)
		# The first MONDAY has to be within the first two weeks
        week1 = mycal[0]
        week2 = mycal[1]
        if week1[calendar.MONDAY] != 0:
            auditday = week1[calendar.MONDAY]
        else:
        # if the first MONDAY isn't in the first week, it must be in the second week
        	auditday = week2[calendar.MONDAY]
print("%10s %2d" % (calendar.month_name[month], auditday))

まとめ:


Python

  1. 例を含む C# 抽象クラスのチュートリアル:抽象化とは?
  2. 例を使用した Python 文字列 strip() 関数
  3. 例を使用した Python 文字列 count()
  4. 例を使用した Python round() 関数
  5. 例を使用した Python map() 関数
  6. Python Timeit() と例
  7. Yield in Python チュートリアル:Generator &Yield vs Return の例
  8. 例を使用したコレクション内の Python カウンター
  9. 例を使用した Python List count()
  10. Python List index() と例
  11. Python - C による拡張プログラミング