[Tutor] Oops:

John Fouhy john at fouhy.net
Tue Dec 18 04:34:17 CET 2007


On 18/12/2007, Jim Morcombe <jmorcombe at westnet.com.au> wrote:
> Now, if I take int(string) the code will work, except it crashes out when
> the data is null.
>
> student.row = int(student.row)
> ValueError: invalid literal for int() with base 10: ''
>
> What is the easiest and recomended way of turning the strings into numerics?

It doesn't crash -- it just throws an exception.  Exceptions are a big
part of python; you should learn to love them :-)

As an example, you could do this:

try:
  student.row = int(student.row)
except ValueError:
  student.row = None

(assuming that setting student.row to None is the appropriate thing to
do when it's blank)

-- 
John.


More information about the Tutor mailing list