[英]How can i list all holidays' date for each country using holidays module in python?
我正在嘗試在許多國家/地區使用 python 中的假期包查找假期的日期,但不是像這樣一一編寫:
import holidays
for date in holidays.UnitedKingdom(years=2011).items():
print(str(date[0]))
for date in holidays.Argentina(years=2011).items():
print(str(date[0]))
我試圖用這樣的函數來做到這一點:
for i in dir(holidays):
for j in range(2010,2016):
holidays.i(years=2011).keys()
我知道使用循環每個國家的名稱都是一個字符串,但我想將其實現為循環,如果您有任何建議或建議,請與我分享。 謝謝你。
首先: pip show holidays
,然后將位置復制到locationVariable
import holidays
import os
locationVariable = ""
countries = [name.split(".py")[0].replace("_"," ") for name in os.listdir(locationVariable+"holidays/countries/") if name.endswith(".py") and not name.startswith("__")]
print("List of countries are : " + str(len(countries)))
for i in countries:
for j in range(2010,2021):
try:
print(getattr(holidays,i.title().replace(" ",""))(years=j).keys())
except:
print("unable to iterate "+i.title().replace(" ",""))
break
似乎名稱的格式不一致(對於Hongkong
),請使用。
如果有幫助,請確保標記已回答。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.