Since we know what the exception is. The email address already exist
in the db (I have unique=True for the email field). I thought that
having 'except: pass' would solve that problem. However, do I need to
catch a certain exception in order for my loop to continue. Because
as of now when the exception happens my loop stops.
Thanks
On May 19, 3:00 pm, Greg <[EMAIL PROTECTED]> wrote:
> Bruno,
> That is originally how I had my code.
>
> ///
>
> import csv
> from myproject.site.models import OrderEmail
>
> reader = csv.reader(open("myfile.csv", "rb"))
> for row in reader:
> try:
> b = OrderEmail(name=row[0], email=row[1], been_sent="0")
> b.save()
> except:
> pass
>
> ////
>
> I think what is going on is that my email field has to be unique.
> When I try to save a record with an email address that is already in
> the db that is when I get directed to the except clause. So that is
> why some records are have an error. Now all I need to know if how to
> have my for loop keep going through the records...and just disregard
> the records that have an email address that is already in the db.
>
> On May 19, 2:38 pm, bruno desthuilliers
>
> <[EMAIL PROTECTED]> wrote:
> > On 19 mai, 20:48, Greg <[EMAIL PROTECTED]> wrote:
>
> > > Hello,
> > > I have the following code:
>
> > > ///
>
> > > import csv
> > > from myproject.site.models import OrderEmail
>
> > > reader = csv.reader(open("myfile.csv", "rb"))
> > > for row in reader:
> > > try:
> > > b = OrderEmail(name=row[0], email=row[1], been_sent="0")
> > > b.save()
> > > except:
>
> > Advice from a long-time Python programmer : *never* use a bare except
> > clause, always specify explicitely what exception(s) you intend to
> > handle.
>
> > > pass
> > > assert False, "End"
>
> > > Whenever I run across the first problem record...my for loop stops and
> > > 'End' is printed. I thought having 'except: pass' meant that the
> > > record would be skipped and it would proceed to read the next line.
>
> > Given your code, that's what should happen. But:
>
> > > However, now when it finds a exception and assert statement is
> > > printed.
>
> > Err... In the above code, the assert statement will be executed
> > whether there's an exception or not. And "assert False" will always
> > raise. This has nothing to do with your error handling in the for
> > loop, and exceptions happening or not.
>
> > > Any suggestions on how I can read every record in the file and if an
> > > error is encountered then have it just skip that record...without
> > > stopping the loop?
>
> > Just remove the assert statement. And by all mean, do yourself (and
> > whoever will have to maintain this code) a favour and don't leave that
> > bare except clause.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---