[Tutor] Does try-except "lock out scope" or similar?
andreengels at gmail.com
Tue Mar 8 07:22:56 CET 2011
On Tue, Mar 8, 2011 at 5:40 AM, Benjamin Serrato
<benjamin.serrato at gmail.com> wrote:
> I wrote a short script to clean up a csv file but had trouble when
> date_time = time.strptime(date_string, "%m/%d/%y")
> would choke on intermittent Null characters in the file. I put it into a
> try-except, but then I found I couldn't do
> del row
> because I receive a "row is not defined" complaint or similar. So, how do I
> run time.strptime() without locking myself out. And, what is the pretty way
> to do all of this, because a couple hours later it looks pretty ugly. I mean
> it turns out I'm rewriting the file anyway so no need to delete the row.
> Benjamin Serrato
To see the problem with your code, think about the following: Suppose
that you go into the 'except' branch. What is the program going to do
_after_ the code in the except branch has been run? The code will go
on with the part after the try...except, and there will 'need' the
variable row again. You will have to add 'continue' to the except, or
put the code below it in an 'else'.
Another issue I see is the 'del row' code itself - it has no real
function, you are deleting something that will be deleted on the next
execution step anyway.
André Engels, andreengels at gmail.com
More information about the Tutor