Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从monthdatescalendar获得的过滤日期给出AttributeError:';列表';对象没有属性';工作日';_Python_List_Datetime_Calendar - Fatal编程技术网

Python 从monthdatescalendar获得的过滤日期给出AttributeError:';列表';对象没有属性';工作日';

Python 从monthdatescalendar获得的过滤日期给出AttributeError:';列表';对象没有属性';工作日';,python,list,datetime,calendar,Python,List,Datetime,Calendar,场景:我正在尝试将一个月中的所有天数传递到一个列表中,在该列表中,我可以在这些天迭代并使用函数,例如weekday() 问题:到目前为止,我能够使用日历将日期或天数传递到列表中。在这两种情况下,当我尝试读取日期并仅输出所需的工作日时,我会得到错误: AttributeError: 'list' object has no attribute 'weekday' 到目前为止我做了什么(在其他帖子的帮助下): r = [foo for foo in calendar.monthcalendar(2

场景:我正在尝试将一个月中的所有天数传递到一个列表中,在该列表中,我可以在这些天迭代并使用函数,例如weekday()

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

AttributeError: 'list' object has no attribute 'weekday'
到目前为止我做了什么(在其他帖子的帮助下):

r = [foo for foo in calendar.monthcalendar(2019, 1)]
list(r)
r = [foo for foo in cal.monthdatescalendar(2019, 1)]
if r[1].weekday() == 1:
    list(r)
这很好,但我需要它更具活力

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)]
所以我试着:

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

r = [foo for foo in cal.monthdatescalendar(2019, 1)]
list(r)
所有这些都很好,但是当我尝试迭代时,比如:

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

r = [foo for foo in cal.monthdatescalendar(2019, 1) if foo[1].weekday() == 1]
list(r)
我得到了前面提到的错误

问题:关于如何正确/更有效地执行此操作,您有什么想法吗


最终目标:是创建一个函数,将月、年、周和工作日作为输入,并返回一个datetime对象。

可能是这样的

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,在这种情况下,您将选择列表的列表,即列出每周,每周也是一个列表

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日开始。

可能是这样的

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,在这种情况下,您将选择列表的列表,即列出每周,每周也是一个列表

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日开始。

问题是返回嵌套列表:

>>> 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)]]
另一个选项是使用,但列表将是平面的:

>>> [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)]
另外,请注意,您可以在创建对象时指定日历的第一天。然后,要获得一周的第一天,您只需获取列表的第一个元素:

>>> 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:
    ...
因为。

问题是返回嵌套列表:

>>> 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)]]
另一个选项是使用,但列表将是平面的:

>>> [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)]
另外,请注意,您可以在创建对象时指定日历的第一天。然后,要获得一周的第一天,您只需获取列表的第一个元素:

>>> 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:
    ...

因为。

也许我不懂一些东西,但这行的重点是什么:
r=[foo for foo in cal.monthdatescalendar(2019,1)]
如果你能写
r=cal.monthdatescalendar(2019,1)
?@Georgy我试图创建一些东西,我可以在以后用条件进行迭代。没有“如果”语句的那一行只是一个测试。主要目标是:r=[foo for foo in cal.monthdatescalendar(2019,1)如果foo[1]。weekday()==1]也许我不理解一些东西,但这行的重点是什么:
r=[foo for foo in cal.monthdatescalendar(2019,1)]
如果你能写
r=cal.monthdatescalendar(2019,1)
?@Georgy我正在尝试创建一些东西,稍后可以使用条件进行迭代。没有“如果”语句的那一行只是一个测试。主要目标是:r=[foo for foo in cal.monthdatescalendar(2019,1)如果foo[1]。weekday()==1]谢谢,itermonthdates允许我使用weekday()属性。非常感谢。谢谢,itermonthdates允许我使用weekday()属性。非常感谢,谢谢你的回答。事实上,最后一部分非常有趣,因为它可以解释其他月份的日期在列表中流失的问题。问题是,我想保持代码的灵活性,所以如果我将工作日设置为((calendar.beday)),那么如果我直接输入其他月份或年份,我就会遇到问题。谢谢你的回答。事实上,最后一部分非常有趣,因为它可以解释其他月份的日期在列表中流失的问题。问题是,我想保持代码的灵活性,所以如果我将工作日设置为((calendar.beday)),那么如果我直接输入其他月份或年份,就会遇到问题。