属性错误:“str”对象没有属性“n”;使用 dateutil 时
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是
dateutil.rrule.weekday
对象的列表(元组),而是一个字符串(不具有
.n
属性)。您可以定义一个字典:
,然后调用它:
另外,请记住,这
相同,请添加逗号:
与如果要从单个元素定义元组
is a list (tuple) of
dateutil.rrule.weekday
objects, whileis a string (that does not have the
.n
attribute).You could define a dictionary:
and then call it:
Also, remember that
is the same as
If you want to define a tuple from single element, add comma:
“规则。” + str(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:
and then pass playdays to byweekday without the brackets, like byweekday=playdays, .....
as eumiro said in his answer it's not necessary :)