我在项目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)
但是这会在
的x = datesclass...
行返回一个错误year, month, day = map(int,self.__effectivedate.split('-'))
提出:
> AttributeError: 'datetime.date' object has no attribute 'split'
当我用相同的语法从一个类似的项目运行到同一行时,它工作得很好。对我做错了什么有什么想法吗?
看起来像是在分配日期时间。Date对象为__effecvedate。你不能在上面调用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']