I am trying to write user input to a csv file and I want to validate that input. I'm experimenting with the date validation right now I keep receiving the error. "w.writerow([name, date, employeeid]) NameError: name 'date' is not defined" and I can't figure out how to fix the error. What's good way to validate raw user input while writing to a csv file so that I don't receive these errors? Thanks for the help.
Here's my code:
import csv
from time import strptime
print "Please fill out the following information:"
with open('userinfo.csv', 'a') as f:
w = csv.writer(f, quoting=csv.QUOTE_ALL)
name = raw_input("Your name:")
def get_date():
while True:
date = raw_input("Please enter a date in MM/DD/YY format:")
try:
parsed = strptime(date, "%m/%d/%Y")
except ValueError as e:
print "Could not parse date: {0}".format(e)
else:
return parsed[:3]
year, month, day = get_date()
employeeid =raw_input("EmployeeID:")
w.writerow([name,date,employeeid])