내 프로그램의 한 부분에서는 사용자가 날짜를 입력해야하며 사전에있는 각 제품에 대해이 날짜를 확인하여 제품이 도착한 날짜와 유효 기간이 일치하는지 확인해야합니다 제품이 사용자가 입력 한 날짜 이전 또는 이후에 만료됩니다.날짜에 추가하여 만기 날짜가 지난 경우 확인
import sys
from string import *
import pickle
import datetime
cheeseDictionary = {}
userInput = ""
def loadProduct(fileName):
global cheeseDictionary
f = open(fileName,"r")
line = f.readline() # Reads line from file
while line:
line = line[:-1]
data = split(line,":") # Splits line when there is a colon
cheeseDictionary[data[0]] = {"date":data[1], "life":data[2], "name":data[3]} # Stores each split item
line = f.readline() # Next line
f.close()
def saveProduct(fileName,cheeseDictionary):
f = open(fileName, "w")
for i in sorted(cheeseDictionary.keys()):
v = cheeseDictionary[i]
f.write("%s:%s:%s:%s\n" % (i, v["date"], v["life"], v["name"]))
f.close()
def printProduct(cheeseDictionary):
print "ID"," ","Date"," ","Life(days)"," ","Name"
for cheese in cheeseDictionary:
print cheese," ",cheeseDictionary[cheese]["date"]," ",cheeseDictionary[cheese]["life"]," ",cheeseDictionary[cheese]["name"]
def addProduct():
global cheeseDicitonary
correct = 0
idInput = ""
dateInput = ""
lifeInput = ""
nameinput = ""
while correct != 1:
idInput = raw_input("Please enter the ID of the cheese to be added. ")
if cheeseDictionary.has_key(idInput):
print ("This ID already exists. Please try again.")
correct = 0
else:
newID = idInput
correct = 1
dateInput = raw_input("Please enter the date of the cheese to be added in the format dd/mm/yyyy. ")
lifeInput = raw_input("Please enter the life of the cheese to be added in days. ")
nameInput = raw_input("Please enter the name of the cheese to be added. ")
cheeseDictionary[idInput] = {"date":dateInput, "life":lifeInput, "name":nameInput}
def checkProduct(cheeseDictionary):
dateCheck = raw_input("Please enter the date in the format dd/mm/yyyy: ")
for cheese in cheeseDictionary:
날짜 저장 형식을 날짜 형식으로 변경해야한다는 것을 알고 있지만 어떻게해야할지 확신이 서지 않습니다. 주어진 조언을 주셔서 감사합니다. :)
예. 그게 내가 시도한 것입니다. 사전에 저장된 날짜로 처리하려고 시도했지만 오류가 계속 발생합니다. AttributeError : '모듈'객체에 'strptime'속성이 없습니다. – Ash
불행히도 'datetime' 모듈과'datetime' 클래스가 둘 다 있습니다. 그 모듈. AttributeError는 클래스가 아닌 모듈을 참조하기 때문에 발생합니다. 'datetime.datetime.strptime (...) '을 사용하여 모듈 대신 클래스에 액세스하십시오. – srgerg