Python - 使用datetime的相同代码行在一个文件中失败但在另一个文件中失败

时间:2015-05-25 16:13:31

标签: python datetime python-3.x

我在项目A中有以下代码行

#filename : mod_dates
#Handles date calculations etc

import datetime

class datecalcs:

    def __init__(self):
        self.__menuChoice = 0
        self.__datemonth = "not set"
        self.__effectivedate = ""
        self.__year = 0
        self.__month = 0
        return None

#
    def interestcouponpaydates(self,effectivedate,couponday):
        self.__effectivedate = effectivedate


        year, month, day = map(int,self.__effectivedate.split('-'))

        print(year)
        print(month)

        return self.__effectivedate

当我使用

从另一个文件中调用它们时
import mod_dates
import datetime
import modcalinputs

datesclass = mod_dates.datecalcs()
calcInputs = modcalinputs.calcinputs()

#Get the coupon date
interestdateeffective = calcInputs.interestdateffective()
interestdatecoupon = calcInputs.interestdatecoupon()


x =     datesclass.interestcouponpaydates(interestdateeffective,interestdatecoupon)
print(x)

然而,这会在<{p>}的x = datesclass...行返回错误

year, month, day = map(int,self.__effectivedate.split('-'))

提出:

> AttributeError: 'datetime.date' object has no attribute 'split'

当我从一个类似的项目运行到具有相同语法的同一行时,它工作正常。关于我做错了什么想法?

1 个答案:

答案 0 :(得分:0)

看起来有些东西正在为__effectivedate分配datetime.date对象。你不能在那上面调用split():

>>> import date
>>> d = d = datetime.date(2012,3,12)
>>> d.split('-')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'datetime.date' object has no attribute 'split' 

您可以将其转换为字符串并拆分:

>>>str(d).split('-')
['2012', '03', '12']