산업 제조
산업용 사물 인터넷 | 산업자재 | 장비 유지 보수 및 수리 | 산업 프로그래밍 |
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. EXAMPLE이 있는 Python String strip() 함수
  3. 예제가 있는 Python 문자열 count()
  4. 예제가 있는 Python round() 함수
  5. 예제가 있는 Python map() 함수
  6. 예제가 있는 Python Timeit()
  7. Python 튜토리얼의 Yield:Generator &Yield vs Return 예제
  8. 예제가 있는 컬렉션의 Python 카운터
  9. 예제가 있는 Python 목록 count()
  10. 예제가 있는 Python 목록 index()