简体   繁体   English

如何使用python日期时间功能/增量?

[英]How to use python date time function/ increment?

I read many of the similar questions, the function is simple nested for loops used for the API, each minute I can call 5 times. 我读了许多类似的问题,该函数是嵌套在API循环中的简单嵌套函数,每分钟我可以调用5次。 So I set the range of 75 for one year data. 因此,我将一年数据的范围设置为75。 Can you guys help me solve the problem? 你们可以帮我解决问题吗? Thanks in advance! 提前致谢!

The first part is working, enter the zip code from the list. 第一部分正常工作,从列表中输入邮政编码。

for zip in zipcode: 
    url4 = url1+str(zip)+url2

Second part is not working 第二部分不起作用

for x in range (0,75):
   counter = x * 5
   startdate += datetime.timedelta(days=counter)
   enddate += datetime.timedelta(days=counter)
   url = url4+startdate+","+enddate+url3
   apifunction(url)

system said: 系统说:

Traceback (most recent call last):
  File "<pyshell#51>", line 3, in <module>
    startdate += datetime.timedelta(days=counter)
AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'

Probably you're importing like this 可能您是这样导入的

from datetime import datetime

This way you import datetime type from datetime module. 这样,您可以从datetime模块导入datetime类型。 And this type of course does not have timedelta type in it. 当然,这种类型没有timedelta类型。

You should import like this: 您应该这样导入:

import datetime

In this case you simply import datetime module, and when you do datetime.timedelta it means that you want to use type timedelta from module datetime 在这种情况下,您只需导入datetime模块,并且在执行datetime.timedelta这意味着您要使用来自模块datetime type timedelta

It looks like datetime is getting redefined somewhere else in your code. 看起来日期时间正在代码中的其他地方重新定义。

>>> import datetime
>>> type(datetime)
<type 'module'>

Your 'datetime' there is an object, which probably means you made a variable and called it datetime somewhere else in your code. 您的“ datetime”有一个对象,这可能意味着您做了一个变量,并在代码中的其他地方将其称为datetime。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM