Manufaturação industrial
Internet das coisas industrial | Materiais industriais | Manutenção e reparo de equipamentos | Programação industrial |
home  MfgRobots >> Manufaturação industrial >  >> Industrial programming >> python

Tutorial Python CALENDAR com Exemplo


O módulo Calendar em Python possui a classe calendar que permite os cálculos para várias tarefas com base em data, mês e ano. Além disso, a classe TextCalendar e HTMLCalendar em Python permite que você edite o calendário e use conforme sua necessidade.

Vamos ver o que podemos fazer com o Python Calendar.

Etapa 1) Execute o código.



Vamos alterar rapidamente o valor de domingo para quinta e verificar a saída



Etapa 2) Você também pode imprimir o calendário em formato HTML, esse recurso é útil para o desenvolvedor se eles quiserem fazer alterações na aparência do calendário



Etapa 3) Faz um loop nos dias de um mês usando c.itermonthday (2025,4), ele buscará o número total de dias desse mês.



Etapa 4) Você pode buscar os dados do sistema local, como meses ou dias da semana, etc.




Etapa 5) Você pode buscar a lista do dia específico para um ano inteiro. Por exemplo, há um dia de auditoria em toda primeira segunda-feira de uma semana. Você quer saber a data da primeira segunda-feira de cada mês. Você pode usar este código



Aqui está o código completo

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

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

Resumo:


python

  1. Tutorial de classe abstrata C# com exemplo:O que é abstração?
  2. Função Python String strip () com EXEMPLO
  3. Python String count() com EXEMPLOS
  4. Função Python round() com EXEMPLOS
  5. Função Python map() com EXEMPLOS
  6. Python Timeit() com exemplos
  7. Yield in Python Tutorial:Generator &Yield vs Return Example
  8. Contador Python em coleções com exemplo
  9. Python List count() com EXEMPLOS
  10. Python List index() com exemplo