AttributeError:“ datetime”模块没有属性“ strptime” [英] AttributeError: 'datetime' module has no attribute 'strptime'

查看:227
本文介绍了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 模块也将其 datetime

#module  class    method
datetime.datetime.strptime(date, "%Y-%m-%d")

这篇关于AttributeError:“ datetime”模块没有属性“ strptime”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆