I am trying to iterate a function over the list returned from rrule and keep getting "object has no attribute 'getitem'" message. Does that mean I first have to "parse" results from rrule to be able to use them for iteration?

Example code

for smoketest in rrule.rrule(rrule.HOURLY, dtstart=startD, until=endD):
  print smoketest
  dateID = smoketest[0:10]
  hourID = smoketest[11:13]

Exact error is TypeError: 'datetime.datetime' object has no attribute 'getitem'.

Sorry if this sounds kind of dumb, This is my first in programming.

有帮助吗?

解决方案

smoketest is a datetime.datetime object, not a string. What you see in print is its string representation. datetime.datetime object offers much more.

Do you need the smoketest as date string and as time string? Use the strftime method:

for smoketest in rrule.rrule(rrule.HOURLY, dtstart=startD, until=endD):
    dateID = smoketest.strftime('%Y-%m-%d')
    hourID = smoketest.strftime('%H:%M:%S')
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top