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

2024-09-28 22:23:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我在项目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'

当我用相同的语法从一个类似的项目运行到同一行时,它工作得很好。你知道我做错了什么吗?你知道吗


Tags: 项目importselfmoddatetimedateyearsplit
1条回答
网友
1楼 · 发布于 2024-09-28 22:23:54

好像有什么东西在分配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']

相关问题 更多 >