例を使用した Python CALENDAR チュートリアル
Python の Calendar モジュールには、日付、月、および年に基づくさまざまなタスクの計算を可能にするカレンダー クラスがあります。それに加えて、Python の TextCalendar および HTMLCalendar クラスを使用すると、カレンダーを編集して、必要に応じて使用できます。
Python Calendar で何ができるか見てみましょう。
ステップ 1) コードを実行します。
- コード 1 行目:このモジュールのすべてのクラスをインポートする「カレンダーのインポート」から始めます。
- コード 3 行目:c=calendar.TextCalendar(calendar.SUNDAY) は、インタプリタにテキスト カレンダーを作成するよう指示します。月初めは日曜日になります。 Python では、月の最初の日を変更できるため、カレンダーをフォーマットできます
- コード 4 行目:str=c.formatmonth(2025,1) 2025 年のカレンダーを作成しています。1 月 1 ~ 1 月
- コード 5 行目:print str は出力を出力します。
値を日曜日から木曜日にすばやく変更して、出力を確認してみましょう
ステップ 2) カレンダーを HTML 形式で印刷することもできます。この機能は、開発者がカレンダーのルック アンド フィールを変更したい場合に役立ちます
ステップ 3) c.itermonthday (2025,4) を使用して月の日数をループし、その月の合計日数を取得します。
- 特定の月の合計日数を取得するコードを実行して「4 月」と表示すると、出力に 30 日が表示されますが、これらの日数とともに最初と、場合によっては最初にゼロが表示されます。
- 出力のゼロは、曜日が重複する月にあることを意味します。つまり、その月に属していないことを意味します。
- これらのゼロが出力に表示されるのは、コードで日 (木曜日) に言及しているため、関数「c.itermonthdays」を呼び出すと、木曜日から日数のカウントが開始され、木曜日が日付 1 から始まらない可能性があるためです st 4 月の 28 th かもしれません または 29 したがって、コードを実行すると、28 th からの日数のカウントが開始されます。 3 月以降は 1 st まで 4月の。これらの日はゼロとしてカウントされ、出力ではこれらのゼロが表示され、同じことが月末にも適用されます。
- したがって、日付 1-30 を除いて、前の月と後の月のすべての日付がゼロとして出力に表示されます。
ステップ 4) 月や曜日など、ローカル システムからデータを取得できます
- ここの出力は、ローカル システムから月名を出力したことを示しています。同様に、以下に示すように曜日名を取得することもできます
- 出力はローカル システムに依存します。ローカル システムが他の国である場合、その国のローカル設定に従って出力が得られるとします。ここでは月なので違いはありませんが、1 週間または 1 日であれば、確実に異なります。
ステップ 5) 年間の特定の日のリストを取得できます。たとえば、毎週第 1 月曜日が監査日です。各月の最初の月曜日の日付を知りたいとします。このコードを使用できます
- mycal =calendar.monthcalendar(2025, month) はその月のカレンダーを作成します
- 変数 week1 と week2 をカレンダーの第 1 週と第 2 週に設定します
- 第 1 週に月曜日が含まれているかどうかを確認し、監査日を設定します
- それ以外の場合は、監査日を第 2 週の最初の月曜日に設定します
- 出力には、その月の最初の月曜日の日付が表示されます。
- この Cal オブジェクトの長さは、1 か月の週数に基づいて一定の長さになります。私たちの場合、週の最初の月曜日が最も頻繁に最初の週になるため、1 つまたは 2 つになりますが、そうでない場合は 2 番目の週を検討してください。 2 週目も検討する理由を詳しく見てみましょう。
- ここでは、カレンダーの定数 Monday を使用しています。calendar オブジェクトは、日曜日、月曜日、火曜日などを表す定数を提供します。これらは以前に見たことがあります。そのため、週 1 で月曜日の定数によって表される日が 0 に等しくない場合、ゼロは別の月に属する日を意味することに注意してください。したがって、この場合、ゼロの場合は、前の月に属する月曜日になります。しかし、最初の月曜日が 0 でない場合、それは私の監査日が 1 週目にあることを意味します。それ以外の場合、それが 0 の場合、最初の月曜日はその月の最初の週ではなく、2 番目の週でなければなりません。
- それでは、監査日の変数を第 2 週で表される月曜日に設定します。したがって、監査日は、第 1 週または第 2 週のいずれかの日が何であれ、戻ってきます。
これが完全なコードです
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 では、月の開始日を変更できるため、カレンダーを自由にフォーマットできます
- カレンダーを HTML 形式で印刷する
- 月や曜日など、ローカル システムからデータを取得する
- 1 年間の特定の日のリストを取得する
Python
- 例を含む C# 抽象クラスのチュートリアル:抽象化とは?
- 例を使用した Python 文字列 strip() 関数
- 例を使用した Python 文字列 count()
- 例を使用した Python round() 関数
- 例を使用した Python map() 関数
- Python Timeit() と例
- Yield in Python チュートリアル:Generator &Yield vs Return の例
- 例を使用したコレクション内の Python カウンター
- 例を使用した Python List count()
- Python List index() と例
- Python - C による拡張プログラミング