我正在编写一个程序,该程序使用从文件中收集信息以稍后输出,我现在的问题是测试给定的信息。我想测试用户输入的代码是否与文件中的日期相同。使用的文件包含格式为 YYYY、MM、DD 的日期和销售额。我正在尝试使用 for 循环针对用户输入测试文件的每一行,但我收到 datetime.date 不可迭代的错误。任何解决方案/替代方案?这是代码,
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 不可迭代。