0

I need to fetch number of holidays for month wise for below code

code

import holidays
import calendar

india_holidays = holidays.India ( years=2022 )
#print(india_holidays)

holiday_count_dict = {}

for date, occassion in india_holidays.items ():
    date = str ( date )
    if date[:7] not in holiday_count_dict:
        holiday_count_dict[date[:7]] = 1
    else:
        val = holiday_count_dict[date[:7]]
        holiday_count_dict[date[:7]] = val + 1
 print ( "holiday count : ", holiday_count_dict )
 print(list(holiday_count_dict.values())[0])

 now = datetime.datetime.now ()
 cal = calendar.Calendar ()
 working_days = len ( [x for x in cal.itermonthdays2 ( now.year, now.month ) if x[0] != 0 and x[1] < 5] ) - (list(holiday_count_dict.values())[0])

Kindly help me how to resolve it

Gomathi
  • 131
  • 9
  • @gajendragarg No error in the code... ** working_days = len ( [x for x in cal.itermonthdays2 ( now.year, now.month ) if x[0] != 0 and x[1] < 5] ) - (list(holiday_count_dict.values())[0])** – Gomathi Feb 25 '22 at 07:54
  • i need to fetch records for monthwise holiday – Gomathi Feb 25 '22 at 07:55
  • if I run your code, it gives me this output: holiday count : {'2022-01': 2, '2022-08': 1, '2022-10': 2, '2022-05': 1, '2022-12': 1, '2022-03': 1} – gajendragarg Feb 25 '22 at 07:55
  • Yes, working_days = len ( [x for x in cal.itermonthdays2 ( now.year, now.month ) if x[0] != 0 and x[1] < 5] ) - (list(holiday_count_dict.values())[0]) – Gomathi Feb 25 '22 at 09:46

0 Answers0