How can Python print the value of an attribute but complain it does not exist?

Dan thermostat at gmail.com
Wed Oct 10 15:34:58 EDT 2007


On Oct 10, 3:03 pm, Emre  Sevinc <emre.sev... at gmail.com> wrote:
> Hello,
>
> I'm a Python newbie and I'm having a strange trouble with the
> following code:
>
> generatefeedvector-debug.py
> ===============================================================
> import feedparser
> import re
>
> def getwordcounts(url):
>     # Parse the feed
>     d = feedparser.parse(url)
>     wc = {}
>
>     # Loop over all the entries
>     for e in d.entries:
>         if 'summary' in e: summary = e.summary
>         else: summary = e.description
>
>         # Extract a list of words
>         words = getwords(e.title + ' ' + summary)
>         for word in words:
>             wc.setdefault(word, 0)
>             wc[word] += 1
>
>     print d.feed.title
>     return d.feed.title
>
> def getwords(html):
>     # Remove all the HTML tags
>     txt = re.compile(r'<[^>]+>').sub('', html)
>
>     # Split words by all non-alpha characters
>     words = re.compile(r'[^A-Z^a-z]+').split(txt)
>
>     # Convert to lowercase
>     return [word.lower() for word in words if word != '']
>
> apcount = {}
> wordcounts = {}
> for feedurl in file('feedlist1-2.txt'):
>     title = getwordcounts(feedurl)
> ==================================================================
>
> When I run it:
>
> $ python generatefeedvector-debug.py
> Signal vs. Noise
> Traceback (most recent call last):
>   File "generatefeedvector-debug.py", line 37, in ?
>     title = getwordcounts(feedurl)
>   File "generatefeedvector-debug.py", line 21, in getwordcounts
>     print d.feed.title
>   File "/var/lib/python-support/python2.4/feedparser.py", line 236, in
> __getattr__
>     raise AttributeError, "object has no attribute '%s'" % key
> AttributeError: object has no attribute 'title'
>
> The strange thing is that it DOES print the value d.feed.title then
> complains AttributeError: object has no attribute 'title'. What am I
> doing wrong?
>
> The file feedlist1-2.txt includes just a single line:
>
> $ cat feedlist1-2.txthttp://feeds.feedburner.com/37signals/beMH
>
> I'm using Python 2.4 on Debian GNU/Linux.
>
> Any ideas about how to fix this error message?
>
> Regards,
>
> --
> Emre Sevinc

This is a bit of a guess, but prehaps the file has a blank line, so
the first url is fine, but the second (non-existant) url doesn't have
a title. You can test this by doing:

> for feedurl in file('feedlist1-2.txt'):
>    if feedurl.strip():
>       title = getwordcounts(feedurl)

-Dan




More information about the Python-list mailing list