首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Python -如何排除Sat。还有Sun。从几周开始

Python -如何排除Sat。还有Sun。从几周开始
EN

Stack Overflow用户
提问于 2018-07-21 00:33:53
回答 3查看 876关注 0票数 3

我希望有人能给我一些返回日历数据的方法的例子,这些日历数据可以做以下事情。

  1. 返回任何给定月份的天数(过去/现在/未来),但不包括周六和周日。

示例:

输入(‘:') -2018年输入

input('Month:') - 7

最终结果:2018年7月的工作日数为(22)。

  1. 在排除Sat之后的每个工作日都会分配一个迭代器。和Sun.

示例:

输入(‘:') -2018年输入

input('Month:') - 7

input('date:') - 20

最终结果: (20)是一个(星期五),是(15) weekday of (7月),(2018)。

这是我到目前为止所能创建的代码...

代码语言:javascript
复制
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打印出文本日历。和Sun,所以我至少可以在视觉上把它们排除在外。但我真的希望能够以编程方式排除它们,并能够输入上述变量来计算一个月中的周日,以及该月内的每一天是哪一周。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2018-07-21 01:44:22

要获取一个月中的工作日数,请执行以下操作:

代码语言:javascript
复制
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

要获取特定日期的工作日数,可以修改上面的代码,以便在到达输入的日期时返回工作日计数。

票数 1
EN

Stack Overflow用户

发布于 2018-07-21 01:46:53

以下是计算前一周天数的一种方法:

请注意,year、month、day的类型转换为int

代码语言:javascript
复制
import calendar

year = int(input('Year: '))
month = int(input('Month: '))
day = int(input('Day: '))

full_wks = day / 7
extra_days = day % 7

first_day = calendar.weekday(year, month, 1)
if first_day >= 5:              # if month begins on a weekend
    weekend = 7 - first_day     # yields 1 if Sunday, 2 if Saturday
    extra_days -= weekend

weekdays = full_wks * 5 + extra_days

ordinal = lambda n: "{}{}".format(n, 'tsnrhtdd'[n%5*(n%100^15>4>n%10)::4])

print "{}/{} is the {} weekday in the month.".format(month, day, ordinal(weekdays))

输出:

代码语言:javascript
复制
Year: 2018
Month: 7
Day: 20
7/20 is the 15th weekday in the month.

Code Golf上从xsot转换成序数。

票数 1
EN

Stack Overflow用户

发布于 2018-07-21 08:26:01

我在大约10分钟内做了一个简单的解决方案。我的方法在很大程度上依赖于列表理解和字符串方法,所以如果你还不熟悉它们,我建议你去查一查。首先将结果拆分成行,标题需要重新居中,其他行需要删除最后一个字符。

通过使用strip()方法将第一行重新居中,删除该行请求时的空格,然后预先添加两个空格。

通过使用列表理解只包含每行的前15个字符来利用周末。

最后的部分是最难的。这个想法是为了计算我的格式化日历中有多少天的数字。首先将包含日期数字的所有行放到一个大行中,然后用空格拆分该行以获得所有日期数字的列表,最后使用该列表的大小。

代码语言:javascript
复制
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]))

lines = result_cal.split("\n") # Split result_cal into a list of lines
title_line = lines[0] # Save first line, we want to edit this differently
title_line = "  " + title_line.strip() # Change the indentation of the title line


lines = lines[1:] # Now select lines below the first 
lines = [line[:3*5] for line in lines] # Only Keep first 15 characters or each
                                       # line to exclude weekends. (5 weekdays *
                                       # 3 chars per day)

lines = [line for line in lines if len(line.strip())] # Don't include empty lines
                                                      # happens if month starts
                                                      # on a weekend.

# prints out the result
print(title_line)
for line in lines:
    print(line)

# Next work out how many working days in month.
nums = "".join(lines[1:]).split(" ") # Three parts: first lines[1:] means this
                                     # only looks at the portion of the calendar
                                     # with numbers written on then. 2nd "".join()
                                     # joins the lines into a single string on 1 line
                                     # 3rd the .split(" ") splits the string based on spaces
                                     # unfortunatly 2 consecutive spaces result in an empty
                                     # string.

nums = [num for num in nums if len(num)] # Filters the empty string.
print(f"There are {len(nums)} working days in the month you selected") # Prints result
#(uses f-string, you may want to look them up, I find them to be very useful)
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51446944

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档