python - Python - 如何排除周六。和太阳。从几周开始
问题描述
我希望有人能给我一些示例,说明我可以返回执行以下操作的日历数据。
- 返回任何给定月份(过去/现在/未来)中的天数,但不包括周六和周日。
例子:
输入('年份:') - 2018
输入('月份:') - 7
最终结果:(2018 年)(7 月)的工作日数为(22)。
- 在排除 Sat 后,为每个工作日分配一个迭代器。和太阳。
例子:
输入('年份:') - 2018
输入('月份:') - 7
输入('日期:') - 20
最终结果: (20) 是 ( Friday ) 并且是 (7), (2018) 的 ( 15 )工作日。
这是我迄今为止能够创建的代码......
import calendar
year = float(input('Year: '))
month = float(input('Month: '))
input_year = []
input_month = []
if year >= 1000 and year <=3000:
input_year.append(year)
if month >= 1 and month <=12:
input_month.append(month)
cal_format = calendar.TextCalendar(calendar.MONDAY)
result_cal = cal_format.formatmonth(int(input_year[0]), int(input_month[0]))
print(result_cal)
THE END RESULT IS...
Year: 1978
Month: 3
March 1978
Mo Tu We Th Fr Sa Su
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
这只会打印出带有 Sat 的文本日历。和太阳在每个周末,所以我至少可以在视觉上排除它们。但我真的很希望能够以编程方式排除它们,并能够输入上述变量来计算一个月中的工作日,以及该月内每天的哪个工作日。
解决方案
要获取一个月中的工作日数:
import calendar
weekdays = 0
cal = calendar.Calendar()
for week in cal.monthdayscalendar(2018, 7):
for i, day in enumerate(week):
# Check if is a weekday and the day is from this month
if i < 5 and day != 0:
weekdays += 1
print weekdays
要获取特定日期的工作日数,您可以修改上述代码以在达到输入日期时返回工作日数。
推荐阅读
- c++ - 将引用类型分配给非引用类型
- python - 如何在pyqt5中修复/隐藏“无法枚举家庭警告”?
- mysql - 带有 convert_tz 的 Mysql str_to_date 返回 NULL
- javascript - 根据用户位置的天气更改布局背景
- mysql - SAAS 软件 - MySQL DB - 分片数据
- python - 我想用 BeautifulSoup 在 python 中抓取,但 'str' 对象没有属性 'find_all' 发生错误
- javascript - 提交表单和 API 调用竞争后,Bootstrap Spinner 不会停止
- api - 我需要 alphaadvantage 的替代品来查看外汇相对于美元的价格
- ios - 在 Project Navigator 中单击 JSON 文件时 Xcode 冻结(并崩溃)
- numpy - 高维广播和