Fabrication industrielle
Internet des objets industriel | Matériaux industriels | Entretien et réparation d'équipement | Programmation industrielle |
home  MfgRobots >> Fabrication industrielle >  >> Industrial programming >> Python

Tutoriel Python CALENDAR avec exemple

Le module de calendrier en Python a la classe de calendrier qui permet les calculs pour diverses tâches en fonction de la date, du mois et de l'année. De plus, les classes TextCalendar et HTMLCalendar en Python vous permettent de modifier le calendrier et de l'utiliser selon vos besoins.

Voyons ce que nous pouvons faire avec Python Calendar.

Étape 1) Exécutez le code.

Modifions rapidement la valeur de dimanche à jeudi et vérifions la sortie

Étape 2) Vous pouvez également imprimer le calendrier au format HTML, cette fonctionnalité est utile pour le développeur s'il souhaite apporter des modifications à l'apparence du calendrier

Étape 3) Boucle sur les jours d'un mois en utilisant c.itermonthday (2025,4), il récupérera le nombre total de jours pour ce mois.

Étape 4) Vous pouvez récupérer les données du système local, comme les mois ou les jours de la semaine, etc.

Étape 5) Vous pouvez récupérer la liste du jour spécifique pour une année entière. Par exemple, il y a une journée d'audit chaque premier lundi de la semaine. Vous voulez connaître la date du premier lundi de chaque mois. Vous pouvez utiliser ce code

Voici le code complet

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

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

Résumé :


Python

  1. Python String strip() Fonction avec EXAMPLE
  2. Python String count() avec des EXEMPLES
  3. Fonction Python round() avec EXEMPLES
  4. Fonction Python map() avec EXEMPLES
  5. Python Timeit() avec des exemples
  6. Rendement en Python Tutoriel :Exemple de générateur et rendement vs retour
  7. Compteur Python dans les collections avec exemple
  8. Python List count() avec des EXEMPLES
  9. Index de liste Python () avec exemple