[Tutor] Trying a csv error

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Feb 10 23:36:03 CET 2006


> I'd like to do the following
> while(1):
>     try:
>         reader.next() ## csv object method
>     except cvs._csv.Error:  ## or something like this
>         print "bad csv record, skipping ...."
>         continue
>     except StopIteration:
>         break
>
> The problem is that python does not recognize the
> error objects and gives me:
> "'module' object has no attribute '_csv'".
>
> Any ideas on how to trap this error?

Hi Tim,

According to the bottom of:

    http://www.python.org/doc/lib/csv-contents.html

you should be able to check for csv.Error.

    """The csv module defines the following exception:

    exception Error
        Raised by any of the functions when an error is detected."""


Here's what it looks like:

######
>>> import csv
>>> csv.Error
<class _csv.Error at 0x81b4f8c>
######

So internally, csv.Error is the thing you've been seeing, but from the
API, you should try to ignore that particular internal implementation
detail, and just use csv.Error instead.


Best of wishes!



More information about the Tutor mailing list