Hi again David
On 16/10/2019 14.22, 'David Turner' via Django users wrote:
> Thanks for pointing me in the right direction.
>
No problem. Happy to help.
Just a pedantic note on your solution:
> I added a simple
> try:
> y = float(row[6])
> except:
> y = None
> And changed the area_hectares=y, in my script.
> Everything works fine now.
>
While this definitely solves the issue, there are some things you might
consider.
Catch *all* exceptions as you do with the "except:" line can be quite
dangerous, since you will not only catch the TypeError that caused the
original problem, but also other exceptions like IndexError or whatever
else might raise an exception in the try block now or in the future when
you update the code.
This might be what you want, but it can cause very hard to debug errors
much later, so it's good to be aware of.
Also, while "asking for forgiveness" as this kind of exception handling
is doing is a perfectly valid pattern in Python code, it might not be
what you want here. What if the field wasn't empty but instead contained
an actual but invalid value?
Again that might be what you want, but you could consider doing
something like:
y = float(row[6]) if row[6] else None
That will explicitly set the variable if row 6 is falsy as an empty
string is.
Kind regards,
Kasper Laudrup
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9e34a9cd-b47c-b26b-8dba-cd27f338d753%40stacktrace.dk.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment