[英]python an integer is required in replace
this is my code 这是我的代码
for date in self.getDateRange():
date = date.replace('-','/')
and this is the getDateRange function: 这是getDateRange函数:
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 太奇怪了,我总是能够进行替换,没有任何问题,我不知道为什么在这里
You're operating on actual date
objects which have their own replace()
method : 您正在使用具有自己的
replace()
方法的实际date
对象进行操作:
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).
例如,如果d == date(2002,12,31),则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()
. 但是由于您已经有了对象,因此可以使用
strftime()
将斜线格式化为分隔符。
The objects in your list are not strings. 列表中的对象不是字符串。 They are datetime objects ( https://docs.python.org/2/library/datetime.html ).
它们是日期时间对象( https://docs.python.org/2/library/datetime.html )。
You should use date.strftime to output your date in whatever format you'd like. 您应该使用date.strftime以所需的任何格式输出日期。 Refer to https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior for the full formatting options.
有关完整的格式设置选项,请参阅https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior 。
For example, you might want date.strftime('%m/%d/%Y') 例如,您可能需要date.strftime('%m /%d /%Y')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.