Python-如何排除Sat和Sun。从几周开始

Python-如何排除Sat和Sun。从几周开始,python,calendar,Python,Calendar,我希望有人能给我举一些例子,说明我可以返回日历数据的方法 返回任何给定月份(过去/现在/将来)的天数, 但不包括周六和周日 示例: 输入('Year:')-2018 输入('月份:')-7 最终结果:2018年7月的工作日数为(22) 排除周六和周日后,为每个工作日分配一个迭代器 示例: 输入('Year:')-2018 输入('月份:')-7 输入('日期:')-20 最终结果:这(20)是一个(周五),是(2018年7月)的(15)工作日 这是我到目前为止能够创建的代码 import c

我希望有人能给我举一些例子,说明我可以返回日历数据的方法


  • 返回任何给定月份(过去/现在/将来)的天数, 但不包括周六和周日 示例:

    输入('Year:')-2018

    输入('月份:')-7

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


  • 排除周六和周日后,为每个工作日分配一个迭代器
  • 示例:

    输入('Year:')-2018

    输入('月份:')-7

    输入('日期:')-20

    最终结果:这(20)是一个(周五),是(2018年7月)的(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
    
    导入日历
    年份=浮动(输入('年份:'))
    月份=浮动(输入('月份:'))
    输入年份=[]
    输入月份=[]
    
    如果年>=1000,年=1,月获取一个月内的工作日数:

    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
    
    导入日历
    工作日=0
    cal=calendar.calendar()
    对于cal.monthdayscalendar的一周(2018年7月):
    对于i,枚举中的天(周):
    #检查是否为工作日,日期是否从本月开始
    如果我<5天!=0:
    工作日+=1
    打印工作日
    

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

    这里有一种方法可以找到以前工作日的数量:

    请注意,年、月、日的类型转换为
    int

    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))
    
    输出:

    Year: 2018
    Month: 7
    Day: 20
    7/20 is the 15th weekday in the month.
    

    从xsot开始的序号转换。

    检查日期工作日的一种方法是使用
    日期
    对象的方法。它位于python标准库
    datetime
    模块中

    日期.工作日()

    以整数形式返回一周中的某一天,其中周一为0,周日为6。例如,
    date(2002,12,4).weekday()==2
    ,一个星期三。另见

    导入日期时间
    工作日的数量=0
    工作日列表=[]
    开始日期=datetime.date(int(输入年份[0]),int(输入月份[0]),1)
    当前日期=开始日期
    当cur_date.month==start_date.month时:
    
    如果010分钟左右,我做了一个简单的溶液。我的方法在很大程度上依赖于列表理解和字符串方法,如join和split,因此如果您还不熟悉它们,我建议您查找它们。首先将结果分成几行,标题需要重新居中,其他行需要删除最后的字符

    使用strip()方法重新居中第一行,删除行间空白,然后预先添加两个空格

    以周末为例,使用列表理解法只包括每行的前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]))
    
    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)
    
    导入日历
    年份=浮动(输入('年份:'))
    月份=浮动(输入('月份:'))
    输入年份=[]
    输入月份=[]
    如果年份>=1000,年份=1,月份
    
    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)