从monthdatescalendar获取的过滤日期给出了AttributeError:'list' object没有属性'weekday'

loading...


0

Scenario: 我试图将一个月中的所有日子传递到一个列表,在这个列表中我可以迭代并使用日期中的函数,例如weekday() .

Issue: 到目前为止,我能够使用日历将日期或日期传递到列表中 . 在这两种情况下,当我尝试读取日期并仅输出所需的工作日时,我收到错误:

AttributeError: 'list' object has no attribute 'weekday'

What I did so far (with the help from other posts here on SO):

这在这里工作正常,但我需要它更有活力

import calendar
cal = calendar.Calendar()
cal.monthdatescalendar(2019, 1)[0][1]
cal.monthdatescalendar(2019, 1)[0][1].weekday()
r = [foo for foo in cal.monthdatescalendar(2019, 1)]

So I tried:

r = [foo for foo in calendar.monthcalendar(2019, 1)]
list(r)

and

r = [foo for foo in cal.monthdatescalendar(2019, 1)]
list(r)

All of these work fine, but when I try to iterate, like:

r = [foo for foo in cal.monthdatescalendar(2019, 1)]
if r[1].weekday() == 1:
    list(r)

or

r = [foo for foo in cal.monthdatescalendar(2019, 1) if foo[1].weekday() == 1]
list(r)

我得到了前面提到的错误 .

Question: 关于如何正确/更有效地做到这一点的任何想法?

Final objective: 是创建一个以月,年,周和工作日作为输入的函数,并返回一个日期时间对象 .

loading...

2回答

  • 1

    可能是这样的

    import calendar
    
    cal = calendar.Calendar(firstweekday=0) # firstweekday is an integer specifying the first day of the week. 0 is Monday (the default), 6 is Sunday.
    for my_date in cal.itermonthdates(year=2019, month=1):
        print('{}. Weekday is {}'.format(my_date.strftime('%A, %Y-%m-%d'), my_date.weekday()))
    

    或者如果你想使用monthdatescalendar,在这种情况下你将列出列表列表,即列出ot周和每周也是一个列表

    import calendar
    
    cal = calendar.Calendar(firstweekday=0) # firstweekday is an integer specifying the first day of the week. 0 is Monday (the default), 6 is Sunday.
    for week in cal.monthdatescalendar(year=2019, month=1):
        for my_date in week:
            print('{}. Weekday is {}'.format(my_date.strftime('%A, %Y-%m-%d'), my_date.weekday()))
    

    请注意,它会暴露整周,例如在这种情况下,它将从2018年12月31日开始 .


  • 1

    问题是monthdatescalendar返回一个嵌套列表:

    >>> import calendar
    >>> cal = calendar.Calendar()
    >>> cal.monthdatescalendar(2019, 1)
    [[datetime.date(2018, 12, 31),
      datetime.date(2019, 1, 1),
      datetime.date(2019, 1, 2),
      datetime.date(2019, 1, 3),
      datetime.date(2019, 1, 4),
      datetime.date(2019, 1, 5),
      datetime.date(2019, 1, 6)],
     [datetime.date(2019, 1, 7),
      datetime.date(2019, 1, 8),
      datetime.date(2019, 1, 9),
      datetime.date(2019, 1, 10),
      ...
    

    因此,如果您想保持相同的结构,为了只检索一周中的第二天,您可以执行以下操作:

    >>> filtered_month = [[day for day in week if day.weekday() == 1]
                          for week in cal.monthdatescalendar(2019, 1)]
    >>> filtered_month
    [[datetime.date(2019, 1, 1)],
     [datetime.date(2019, 1, 8)],
     [datetime.date(2019, 1, 15)],
     [datetime.date(2019, 1, 22)],
     [datetime.date(2019, 1, 29)]]
    

    另一种选择是使用itermonthdates,但列表将是平的:

    >>> [day for day in cal.itermonthdates(2019, 1) if day.weekday() == 1]
    [datetime.date(2019, 1, 1),
     datetime.date(2019, 1, 8),
     datetime.date(2019, 1, 15),
     datetime.date(2019, 1, 22),
     datetime.date(2019, 1, 29)]
    

    另请注意,您可以在创建Calendar对象时指定日历的第一天 . 然后,要获得一周的前几天,您只需要获取列表的第一个元素:

    >>> cal = calendar.Calendar(calendar.TUESDAY)
    >>> [week[0] for week in cal.monthdatescalendar(2019, 1)]
    [datetime.date(2019, 1, 1),
     datetime.date(2019, 1, 8),
     datetime.date(2019, 1, 15),
     datetime.date(2019, 1, 22),
     datetime.date(2019, 1, 29)]
    

    P.S.:
    作为旁注,我认为最好明确比较这样的日子:

    if day.weekday() == calendar.TUESDAY:
        ...
    

    代替

    if day.weekday() == 1:
        ...
    

    因为Explicit is better than implicit .

评论

暂时没有评论!