【问题标题】:How can i list all holidays' date for each country using holidays module in python?如何使用python中的假期模块列出每个国家/地区的所有假期日期?
【发布时间】:2020-02-20 08:41:09
【问题描述】:

我正在尝试在 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()

我知道使用循环每个国家的名称都是一个字符串,但我想将它实现为一个循环,如果您有任何建议或建议,请与我分享。 谢谢。

【问题讨论】:

  • 请在问题中添加一个示例,说明您的预期输出应该是什么样的

标签: python python-3.x python-holidays


【解决方案1】:

首先: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),请使用。

如果有帮助,请确保标记已回答。

【讨论】:

  • 谢谢你的回答,对我很有帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-28
  • 2015-08-30
  • 1970-01-01
  • 1970-01-01
  • 2020-05-02
  • 1970-01-01
  • 2011-05-17
相关资源
最近更新 更多