[Tutor] csv module not raising exception properly [SOLVED]

David Rock david at graniteweb.com
Thu Apr 7 23:25:15 CEST 2005


* David Rock <david at graniteweb.com> [2005-04-07 15:00]:
> * Kent Johnson <kent37 at tds.net> [2005-04-07 15:28]:
> > David Rock wrote:
> > >I am trying to catch an exception from the csv module but it doesn't
> > >seem to be generating a proper exception because I don't seem to be able
> > >to catch it. Here is what I am doing:
> > >
> > >
> > >for inputline in fileinput.input(args):
> > >	try:
> > >		input = csv.reader([inputline], escapechar='\\')
> > >	except:
> > >		print "ERROR", inputline
> > >
> > >
> > >This is the traceback:
> > >Traceback (most recent call last):
> > >  File "/usr/openv/local/bin/bpdbreport_test.py", line 539, in ?
> > >    for line in input:
> > >_csv.Error: newline inside string
> > 
> > I'm suspicious of this - the traceback doesn't match the code you show. Is 
> > your program called bpdbreport_test.py? What is happening at line 539 of 
> > bpdbreport_test.py?
> 
> Line 539 is the csv.reader line. I just boiled it down to the place
> where things don't make sense. I use try/except blocks all the time and
> I even tested this one by putting a "raise" statement inside the block
> and it picked it up, so it's not a scope issue or anything weird like
> that. I think the csv module doesn't raise an exception properly. Can
> someone verify that it works for them?
> 
> I am going to continue testing with an extremely watered down version to
> see if it _ever_ works right.

Turns out the REAL problem is when you try and USE the reader object,
NOT when you create it. The documantation was not clear on which
operation would actually generate an error. The traceback actually
alludes to this, in hindsight (Thanks Kent). The input = csv.reader() is
fine. When you try to iterate over the object, THAT'S when it dies.

-- 
David Rock
david at graniteweb.com
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://mail.python.org/pipermail/tutor/attachments/20050407/3039c3ef/attachment.pgp


More information about the Tutor mailing list