属性错误:“str”对象没有属性“n”;使用 dateutil 时

发布于 2025-01-03 14:15:17 字数 790 浏览 0 评论 0原文

我正在使用 dateutil rrule 函数..我从 django 中的数据模型中获取工作日。当我将它放入像这样的 rrule 函数中时

for x in lgs:
    teams = Team.objects.filter(league=x.id)
    teamcount = len(teams)
    playdays = "rrule." + str(x.play_days)
    fixdays = rrule.rrule(rrule.DAILY,byweekday = (playdays),dtstart=startdate,until=endate)
    print list(fixdays)

Traceback (most recent call last):
File "<console>", line 5, in <module>
File "/usr/lib/pymodules/python2.7/dateutil/rrule.py", line 345, in __init__
    elif not wday.n or freq > MONTHLY:
AttributeError: 'str' object has no attribute 'n'

,如果我正常使用该函数,并将第 5 行替换为

    fixdays = rrule.rrule(rrule.DAILY,byweekday = (rrule.SU, rrule.MO),dtstart=startdate,until=endate)

i 就没有问题了。可能出了什么问题。

I am using dateutil rrule function .. i get the weekdays from my data model in django. when i put it in rrule function like this

for x in lgs:
    teams = Team.objects.filter(league=x.id)
    teamcount = len(teams)
    playdays = "rrule." + str(x.play_days)
    fixdays = rrule.rrule(rrule.DAILY,byweekday = (playdays),dtstart=startdate,until=endate)
    print list(fixdays)

Traceback (most recent call last):
File "<console>", line 5, in <module>
File "/usr/lib/pymodules/python2.7/dateutil/rrule.py", line 345, in __init__
    elif not wday.n or freq > MONTHLY:
AttributeError: 'str' object has no attribute 'n'

if i use the function normally, and replace line 5 with

    fixdays = rrule.rrule(rrule.DAILY,byweekday = (rrule.SU, rrule.MO),dtstart=startdate,until=endate)

i get no problem. what could be wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

罪歌 2025-01-10 14:15:17
(rrule.SU, rrule.MO)

dateutil.rrule.weekday 对象的列表(元组),而

"rrule." + str(x.play_days)

是一个字符串(不具有 .n 属性)。

您可以定义一个字典:

weekday = {'MO': rrule.MO, 'TU': rrule.TU, … }

,然后调用它:

playdays = weekday[str(x.play_days)]

另外,请记住,这

byweekday = (playdays)

相同,请添加逗号:

byweekday = playdays

与如果要从单个元素定义元组

byweekday = (playdays,)
(rrule.SU, rrule.MO)

is a list (tuple) of dateutil.rrule.weekday objects, while

"rrule." + str(x.play_days)

is a string (that does not have the .n attribute).

You could define a dictionary:

weekday = {'MO': rrule.MO, 'TU': rrule.TU, … }

and then call it:

playdays = weekday[str(x.play_days)]

Also, remember that

byweekday = (playdays)

is the same as

byweekday = playdays

If you want to define a tuple from single element, add comma:

byweekday = (playdays,)
云雾 2025-01-10 14:15:17

“规则。” + str(x.play_days) 返回一个字符串,而不是具有该名称的变量,而在您的硬编码示例中,您传递了一组变量

您可能想做这样的事情:

playdays = [getattr(rrule, pd) for pd in x.play_days]

然后将 playdays 传递给 byweekday(不带括号),比如byweekday=playdays,......
正如 eumiro 在他的回答中所说,这是没有必要的:)

"rrule." + str(x.play_days) returns a string not the variable with that name, while in your hardcoded example you're passing a set of variables

You probably want to do something like this instead:

playdays = [getattr(rrule, pd) for pd in x.play_days]

and then pass playdays to byweekday without the brackets, like byweekday=playdays, .....
as eumiro said in his answer it's not necessary :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文