6

I wrote this tiny Python snippet that scrapes a feed and prints it out. When I run the code, something in the feed triggers the error message you see here as my question. Here's the complete console output on error:

> Traceback (most recent call last):  
> File "/home/vijay/ffour/ffour5.py",
> line 20, in <module>
>     myfeed()   File "/home/vijay/ffour/ffour5.py", line
> 15, in myfeed
>     sys.stdout.write(entry["title"]).encode('utf-8')
> AttributeError: 'NoneType' object has
> no attribute 'encode'
1
  • 1
    Please supply the code. The error could stem from any number of problems. Commented Jan 5, 2009 at 19:36

2 Answers 2

12
> sys.stdout.write(entry["title"]).encode('utf-8')

This is the culprit. You probably mean:

sys.stdout.write(entry["title"].encode('utf-8'))

(Notice the position of the last closing bracket.)

Sign up to request clarification or add additional context in comments.

Comments

5

Lets try to clear up some of the confusion in the exception message.

The function call

sys.stdout.write(entry["title"])

Returns None. The ".encode('utf-8')" is a call to the encode function on what is returned by the above function.

The problem is that None doesn't have an encode function (or an encode attribute) and so you get an attribute error that names the type you were trying to get an attribute of and the attribute you were trying to get.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.