How to catch exceptions elegantly in this situation?

Mel Wilson mwilson at
Wed Oct 13 15:04:38 CEST 2004

In article <e431ae34.0410122224.25379264 at>,
syed_saqib_ali at (Saqib Ali) wrote:
>Jeremy Bowers <jerf at> wrote in message news:<pan.2004. at>...
>> On Thu, 07 Oct 2004 20:13:06 -0700, Saqib Ali wrote:
>> Can you give me more detail? What is the real problem?
>The real issue is that I am doing some screen-scraping from on-line
>white pages (residential telephone directory).
>I have defined a bunch of regular expressions and myFunc() populates a
>dictionary corresponding to each result from the white pages.
>So essentially myDict corresponds to a single record found. See below
>myDict["fullName"] = fullNameRegExp.match(htmlText)[0]
>myDict["telNum"] = telNumRegExp.match(htmlText)[0]
>myDict["streetAddr"] = streetAddrRegExp.match(htmlText)[0]
>myDict["city"] = cityRegExp.match(htmlText)[0]
>myDict["state"] = stateRegExp.match(htmlText)[0]
>myDict["zip"] = zipRegExp.match(htmlText)[0]
>Sometimes one or more of these regexps fails to match. In which Case
>an exception will be raised. I want to catch the exception, print out
>a message..... but then keep on going to the next assignment

I'd be tempted to try

    redict = {"fullname": fullNameRegExp,
              "telNum": telNumRegExp,
              "streetAddr": streetAddrRegExp,
              "city": cityRegExp,
              "state": stateRegExp,
              "zip": zipRegExp,

    for x in redict:
            myDict[x] = redict[x].match (htmlText)[0]
        except whichever_error:

        Regards.        Mel.

More information about the Python-list mailing list