我正在编写一个程序,它使用一个从文件收集信息以便以后输出,我现在的问题是测试给定的信息。我想测试用户输入的代码是否与文件中的日期相同。使用的文件包含YYYY,MM,DD格式的日期和销售额。我尝试使用for循环来根据用户输入测试文件的每一行,但是我得到的错误是日期时间.日期是不可忍受的。有什么解决方案/替代方案吗?这是密码from datetime import date
def DateTest(Date, Position):
firstTry = True
validSyntax = False
if validSyntax == False:
if firstTry == True:
try:
Date = Date.strip().split(',')
Year = int(Date[0])
Month = int(Date[1])
Day = int(Date[2])
Date = date(Year, Month, Day)
except:
print "That is invalid input."
firstTry = False
else:
validSyntax = True
elif firstTry == False:
Date = raw_input("Please input the desired %s date in the form YYYY,MM,DD: " % Position)
try :
Date = startDate.strip().split(',')
Year = int(Date[0])
Month = int(Date[1])
Day = int(Date[2])
Date = date(Year, Month, Day)
except:
print "That is invalid input."
else:
validSyntax = True
print" ok got it"
if validSyntax == True:
for line in Date:
line = line.strip().split(',')
yearTest = int(line[0])
monthTest = int(line[1])
dayTest = int(line[2])
dateTest = date(yearTest, monthTest, dayTest)
if dateTest == Date:
"print debug"
startDate = raw_input("Please input the desired start date: ")
start = "start"
Response = DateTest(startDate, start)
如您所见,我测试有效输入,然后测试文件中的日期,这告诉我datetime不可读取。在