1

this is my code

for date in self.getDateRange():
          date = date.replace('-','/')

and this is the getDateRange function:

def getDateRange(self):
    from datetime import date, datetime, timedelta
    return self.perdelta(date(2000, 01, 01), date(2015, 8, 03), timedelta(days=1))

def perdelta(self, start, end, delta):
    curr = start
    while curr < end:
        yield curr
        curr += delta

and this is the error message

MySpider.py", line 19, in parse
            date = date.replace('-','/')
        exceptions.TypeError: an integer is required

it is weird, i always able to do the replace, without any problem, i don't know why is here

2 Answers 2

4

You're operating on actual date objects which have their own replace() method:

date.replace(year, month, day)

Return a date with the same value, except for those parameters given new values by whichever keyword arguments are specified. For example, if d == date(2002, 12, 31), then d.replace(day=26) == date(2002, 12, 26).

That methods only takes integers. If you want to work on dates as strings, you need to convert them. But since you already have objects, you can just format them with a slash as a separator using strftime().

Sign up to request clarification or add additional context in comments.

3 Comments

i don't need option of date, i need string of date in this format 04/08/2015
could you give me example please
Then you can just do yield curr.strftime('%m/%d/%Y') in the perdelta() function (update format however you need it).
0

The objects in your list are not strings. They are datetime objects (https://docs.python.org/2/library/datetime.html).

You should use date.strftime to output your date in whatever format you'd like. Refer to https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior for the full formatting options.

For example, you might want date.strftime('%m/%d/%Y')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.