0

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])
kittenparade
  • 83
  • 1
  • 1
  • 10
  • 1
    Don't you actually want `w.writerow([name, year, month, day, employeeid])`? `date` is only defined **inside `get_date`**. – jonrsharpe Oct 26 '15 at 17:12
  • I want it to write all together in MM/DD/YYYY format but I don't know how I need to define date so that it will actually write to the file. – kittenparade Oct 26 '15 at 17:18

1 Answers1

3

date was defined in an inner scope (in get_date()) and it will be deleted when you exit the get_date(), since the interpreter determines that date a local variable of get_date().

You need to return date from get_date() if you want to use it.

Note: date won't be deleted when exit if it was initially defined in a loop or branch instead of function. See Jeremy's answer to Python's scoping

BTW, shouldn't it be MM/DD/YYYY format for strptime?

Community
  • 1
  • 1
stanleyli
  • 1,427
  • 1
  • 11
  • 28
  • I see where date is defined at but how do I return date from get_date()? – kittenparade Oct 26 '15 at 17:26
  • @just `return parsed[:3], date`. This will return them as a tuple of tuple. And say, do `(year, month, day), _date = get_date()` to receive them. – stanleyli Oct 26 '15 at 17:30
  • I don't think I'm understanding because I made those changes to the two lines and am still receiving the 'date' is not defined error. Can you show me specifically how it will look in my code? – kittenparade Oct 26 '15 at 17:34
  • You need to print `_date` instead of `date` in my example. I just use a different variable name to avoid confusion. – stanleyli Oct 26 '15 at 17:36
  • There shouldn't be any problem. If you still experience any issue, just edit your original post by pasting your full code. – stanleyli Oct 26 '15 at 17:42
  • You were not following my suggestion. See my comments: do a `(year, month, day), _date = get_date()` to receive them. – stanleyli Oct 26 '15 at 17:48
  • And you'd better preserve your old code in your post as well to avoid causing any confusion to other guys who saw this question. – stanleyli Oct 26 '15 at 17:50
  • Could you please show me within my code? I thought that's what I did in line 17. I'm really not understanding what change I'm supposed to make. – kittenparade Oct 26 '15 at 17:51
  • No, your line 17 is `year, month, day = get_date(), _date = get_date()` but what I said is `(year, month, day), _date = get_date()` – stanleyli Oct 26 '15 at 17:52
  • Oh sorry. I confused myself because I made that change in my program but missed it on here. I have the parentheses like that in my actual program and it's still not working. – kittenparade Oct 26 '15 at 17:54
  • Try to `print _date` immediately after line 17 and you should see it printed out correctly. I just gave a try at my local and I am 100% sure that it's right. – stanleyli Oct 26 '15 at 17:56
  • Okay that worked for me. But that's not where I'm having the problem. What's wrong is that is won't write to the CSV file. I get the error "w.writerow([name, date, results]) NameError: name 'date' is not defined. – kittenparade Oct 26 '15 at 17:59
  • Because you need to use `_date` instead of `date`. – stanleyli Oct 26 '15 at 18:01
  • OK, if `_date` caused so much trouble for you, just rename EVERY `_date` variable to `date` in your code, and you should be good. – stanleyli Oct 26 '15 at 18:24
  • The reason that I was using `_date` is to show you the different lifespans of `_date` and `date`, so that you can understand how `date` is deleted and `_date` is present outside of the `get_date()`. If you just want the code to work w/o understanding the details, simply use `date` everywhere. Just forget about `_date`. – stanleyli Oct 26 '15 at 18:33
  • I'm so stupid! I finally got it to work! Thanks so much! – kittenparade Oct 26 '15 at 21:32