Industrielle Fertigung
Industrielles Internet der Dinge | Industrielle Materialien | Gerätewartung und Reparatur | Industrielle Programmierung |
home  MfgRobots >> Industrielle Fertigung >  >> Industrial programming >> Python

Python CALENDAR Tutorial mit Beispiel

Das Kalendermodul in Python verfügt über die Kalenderklasse, die die Berechnungen für verschiedene Aufgaben basierend auf Datum, Monat und Jahr ermöglicht. Darüber hinaus können Sie mit den Klassen TextCalendar und HTMLCalendar in Python den Kalender bearbeiten und gemäß Ihren Anforderungen verwenden.

Mal sehen, was wir mit Python Calendar machen können.

Schritt 1) Führen Sie den Code aus.

Lassen Sie uns schnell den Wert von Sonntag auf Donnerstag ändern und die Ausgabe überprüfen

Schritt 2) Sie können den Kalender auch im HTML-Format ausdrucken. Diese Funktion ist hilfreich für Entwickler, wenn sie Änderungen am Erscheinungsbild des Kalenders vornehmen möchten

Schritt 3) Durchläuft die Tage eines Monats mit c.itermonthday (2025,4) und ruft die Gesamtzahl der Tage für diesen Monat ab.

Schritt 4) Sie können die Daten aus dem lokalen System abrufen, wie Monate oder Wochentage usw.

Schritt 5) Sie können die Liste des bestimmten Tages für ein ganzes Jahr abrufen. So findet beispielsweise an jedem ersten Montag einer Woche ein Prüfungstag statt. Sie möchten das Datum des ersten Montags für jeden Monat wissen. Sie können diesen Code verwenden

Hier ist der vollständige Code

Python 2-Beispiel

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-Beispiel

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))

Zusammenfassung:


Python

  1. C# Abstract Class Tutorial mit Beispiel:Was ist Abstraktion?
  2. Python String strip() Funktion mit BEISPIEL
  3. Python String count() mit BEISPIELE
  4. Python-Funktion round() mit BEISPIELE
  5. Python map() Funktion mit BEISPIELE
  6. Python Timeit() mit Beispielen
  7. Yield in Python Tutorial:Generator &Yield vs. Return Beispiel
  8. Python-Zähler in Sammlungen mit Beispiel
  9. Python List count() mit BEISPIELE
  10. Python List index() mit Beispiel