How to catch exceptions elegantly in this situation?

Mel Wilson mwilson at the-wire.com
Wed Oct 13 09:04:38 EDT 2004


In article <e431ae34.0410122224.25379264 at posting.google.com>,
syed_saqib_ali at yahoo.com (Saqib Ali) wrote:
>Jeremy Bowers <jerf at jerf.org> wrote in message news:<pan.2004.10.08.02.50.53.714563 at jerf.org>...
>> On Thu, 07 Oct 2004 20:13:06 -0700, Saqib Ali wrote:
>>
>>
>> Can you give me more detail? What is the real problem?
>>
>
>
>Sure.
>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
>statement.

I'd be tempted to try

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

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



        Regards.        Mel.



More information about the Python-list mailing list