Dnia wtorek, 6 grudnia 2005 16:29, Jan Eden napisaƂ:
> Hi,
>
> I use the following loop to parse some HTML code:
>
> for record in data:
>     try:
>         parser.feed(record['content'])
>     except HTMLParseError, (msg):
>         print "!!!Parsing error in", record['page_id'], ": ", msg
>
> Now after HTMLParser encounters a parse error in one record, it repeats to
> execute the except statement for all following records - why is that?

Short answer: because you told Python to do so...

Long answer:

My hint for students having such problems is to execute their code with a 
pencil on a hardcopy. They read aloud what the program currently does  - 
usually they spot the error during the first "reading".

Your code being "read loud"

1. begin loop
2.  attempt to execute parser.feed
3.   abort attempt if it fails, showing the error
4. take next loop

So - you take next loop regardless of the failure or not. There are two ways 
out of here. I wrote them "aloud", to transcribe into python as an excersize:

(Notice the difference between this and your original)

I)

1. attempt to 
2.  begin loop
3.   abort attempt if it fails, showing the error
4.  take next loop

II)
1. begin loop
2.  attempt to execute parser.feed
3.   abort attempt if it fails, showing the error AND breaking the loop
4. take next loop

Hope this helps,
-- 
 Pawel Kraszewski
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to