중복 티켓을 사용할 수 없도록 손님이 체크인 할 때 티켓 번호의 유효성을 검사하기위한 간단한 비영리 기금 모금 활동 프로그램을 만들고 있습니다. Windows 10 시스템에서 Python 3.4.3을 실행하고 있습니다. 프로그램이 완료되면 기금 모금 행사에서 터치 스크린이있는 라즈베리 파이 (Raspberry Pi)에 사용됩니다.TypeError : 'DictWriter'객체가 반복 가능하지 않습니다.
나는리스트를 만들고, 저장하고, 중복을 검색하기 위해 몇 가지 다른 방법을 시도했다. 이상적으로 목록은 CSV 파일에 저장되지만 일반 텍스트 또는 다른 형식도 괜찮습니다.
중복 티켓을 사용할 수 없도록 파일에 저장된 티켓과 티켓 #을 비교하는 루핑 기능으로 인해 추적 오류 (TypeError : 'DictWriter'개체를 반복 할 수 없음)를 사용할 수 있습니까?
도움을 주셔서 감사합니다. csv.DictWriter
클래스의
version = "v1.4"
fname="tickets.csv"
import csv
import datetime
import os.path
print("\nWelcome to TicketCheck", version)
extant = os.path.isfile(fname)
with open(fname, 'a', newline='') as csvfile:
fieldnames = ['ticketid', 'timestamp']
ticketwriter = csv.DictWriter(csvfile, fieldnames=fieldnames)
if extant == False:
ticketwriter.writeheader()
while True:
ticket = ""
print("Please enter a ticket # to continue or type exit to exit:")
ticket = str(input())
if ticket == "":
continue
if ticket == "exit":
break
print("You entered ticket # %s." % (ticket))
print("Validating ticket...")
for row in ticketwriter:
if row[0] == ticket:
print("\n\n\n===== ERROR!!! TICKET # %s ALREADY CHECKED IN =====\n\n\n" % (ticket))
continue
time = datetime.datetime.now()
print("Thank you for checking in ticket # %s at %s \n\n\n" % (ticket, time))
print("Ticket is now validated.")
ticketwriter.writerow({'ticketid': ticket, 'timestamp': time})
csvfile.flush()
continue
csvfile.close()
print("All your work has been saved in %s.\n Thank you for using TicketCheck %s \n" % (fname, version))