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

E

Emre Sevinc

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.txt
http://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,
 
D

Dan

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,

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top