0

I have seen a lot of KeyCount Errors online but none of them quite match the troubles that I'm having. I am using feed parser to try and create a one run application that accesses all the URLs in a text file and outputs all the entries in each URL. When I run this code :

     import feedparser as f

     with open('addresses.rtf', 'r') as addresses:
         for line in addresses:
             d = f.parse(line)

            print d["feed"]["title"]
             print ""
             print d.feed.subtitle
             print ""

             for post in d.entries:
                 print post.title
                 print post.link
                 print ""

I get this error message :

     Traceback (most recent call last):
       File "/Users/Josh/Desktop/Feed Parser Python Project/init.py", line 7, in <module>
print d["feed"]["title"]
       File "build/bdist.macosx-10.6-intel/egg/feedparser.py", line 375, in __getitem__
return dict.__getitem__(self, key)
     KeyError: 'title'

My text file is just a .rtf file that has a URL on each line (3 lines).

If someone could give us a hand please let me know and if you need any extra info please don't hesitate to ask. Any help is welcome. Thank you!

iruvar
  • 22,736
  • 7
  • 53
  • 82
Joshua Boddy
  • 83
  • 1
  • 9

1 Answers1

3

It's hard to tell exactly what is wrong here, but in the general case, any KeyError is because the data you are trying to access is not exactly what you expected. It's best to throw your assumptions out the window and take a close look at the actual data that your code is working with.

For debugging, I would recommend taking a close look at what happens before the error. What is the value of line as you read the file? Is it correct? What is the value of d? Did the call to f.parse(line) result in a valid object?

Aurora
  • 4,384
  • 3
  • 34
  • 44