datetime - 导入错误: cannot import name 'strftime'

标签 datetime python-3.x

我收到错误,不知道为什么,我的代码如下:

year1 = int(input('choose a year :',))
month1 = int(input('choose a month :',))
day1 = int(input('choose a day :',))
year2 = int(input('choose a year :',))
month2 = int(input('choose a month :',))
day2 = int(input('choose a day :',))
from datetime import date, timedelta as td
from datetime import strftime as st
d1 = date(year1, month1, day1)
d2 = date(year2, month2, day2)
delta = d2 - d1
for i in range(delta.days + 1):
day = st((d1 + td(days=i)))
print(get_exrates(day))    

我得到的完整错误代码是:

Traceback (most recent call last):
    line 17, in <module>
    from datetime import strftime as st
ImportError: cannot import name 'strftime'

最佳答案

strftimedatetime 类的实例方法,您需要先从 datetime 模块导入 datetime,然后使用其 strftime 方法:

>>> from datetime import datetime
>>> datetime.strftime
<method 'strftime' of 'datetime.date' objects>

关于datetime - 导入错误: cannot import name 'strftime' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29698162/

相关文章:

Python: "ImportError: DLL load failed: The specified module could not be found."导入 ffn(Python 的金融库)时出现问题

python - 在 Python 3.x 中使用两个文件对象读写文件

algorithm - 计算日期时间轴最小、最大和主要次要刻度间隔

node.js - Stripe – "Invalid timestamp: can be no more than five years in the future"

python - 在 Pandas 中将 GroupBy 与 DateTime 结合使用 (Python)

Django似乎以UTC格式显示日期时间

python-3.x - 使用python 3.6安装自定义构建tensorflow 1.12车轮时出错

python - 有没有办法使用 cv2.approxPolyDP 来近似开放曲线?

python - python 对包含字符串和整数的列表中的元素求和,并将答案放入另一个列表中

mysql - 使用两个列范围从 mysql 中选择数据