AttributeError: 'datetime' 模块没有属性 'strptime' [英] AttributeError: 'datetime' module has no attribute 'strptime'
本文介绍了AttributeError: 'datetime' 模块没有属性 'strptime'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的 Transaction
类:
class Transaction(object):
def __init__(self, company, num, price, date, is_buy):
self.company = company
self.num = num
self.price = price
self.date = datetime.strptime(date, "%Y-%m-%d")
self.is_buy = is_buy
当我尝试运行 date
函数时:
And when I'm trying to run the date
function:
tr = Transaction('AAPL', 600, '2013-10-25')
print tr.date
我收到以下错误:
self.date = datetime.strptime(self.d, "%Y-%m-%d")
AttributeError: 'module' object has no attribute 'strptime'
我该如何解决?
推荐答案
如果让我猜的话,你是这样做的:
If I had to guess, you did this:
import datetime
在代码的顶部.这意味着你必须这样做:
at the top of your code. This means that you have to do this:
datetime.datetime.strptime(date, "%Y-%m-%d")
访问 strptime
方法.或者,您可以将导入语句更改为:
to access the strptime
method. Or, you could change the import statement to this:
from datetime import datetime
并按原样访问它.
制作datetime
模块的人还命名了他们的 class datetime
:
The people who made the datetime
module also named their class datetime
:
#module class method
datetime.datetime.strptime(date, "%Y-%m-%d")
这篇关于AttributeError: 'datetime' 模块没有属性 'strptime'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文