scanning for numerals / letters

Steve Bergman steve at rueb.com
Tue Apr 18 21:30:49 EDT 2006


Something like this should work:

==================
for c in form.get('date'):
  if c in string.letters:
    print "ERROR: You have to enter a date with numbers."
==================

You have to import the string module.  'letters' is one of the
attributes defined in that module.

Other attributes defined there include 'digits'. (Hint, hint. ;-) )

Also note that if you are not sure if a dictionary has a particular
key, you can use:

form.get('date')

and you will get the value if the key 'date' exists.  If not, you get
None.

It's handier than checking has_key.

You can also say:

form.get('date', '01/01/70')

If the dictionary has a key 'date', you will get the value associated
with it.  Othewise, you will get '01/01/70'.




More information about the Python-list mailing list