Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to resolve AttributeError: 'NoneType' object has no attribute 'encode' in python

for comment_entry in comment_feed.entry:
content = comment_entry.ToString()
parse = BeautifulSoup(content)
for con in parse.find('ns0:content'):
    print con.string
    s = con.string
    file.write(s.encode('utf8'))

Error which I'm getting:

File "channel_search.py", line 108, in youtube_search
file.write(s.encode('utf8'))
AttributeError: 'NoneType' object has no attribute 'encode'
like image 344
user3844662 Avatar asked Sep 22 '14 06:09

user3844662


People also ask

Why do I get AttributeError NoneType object has no attribute something '?

You are getting AttributeError: 'NoneType' object has no attribute 'something' because NoneType means that instead of an instance of whatever Class or Object you think you're working with, you've actually got None. It means that an assignment or function call up above failed or returned an unexpected result.

What is NoneType object has no attribute text?

This error happens when you try to call a method from an object that is None or not initiated. Before calling a method, you need to check if the object is None or not to eliminate this error; after that, call the desired method.

Why do I get NoneType in Python?

The TypeError: 'NoneType' object is not iterable error is raised when you try to iterate over an object whose value is equal to None. To solve this error, make sure that any values that you try to iterate over have been assigned an iterable object, like a string or a list.

How do you handle NoneType?

One way to avoid this error is to check before iterating on an object if that object is None or not. Another way to handle this error is to write the for loop in try-except block. The third way is to explicitly assign an empty list to the variable if it is None .


1 Answers

Your s might be Nonetype

Try

s = con.string
if s:file.write(s.encode('utf8'))
# or if s is not None        
#if you want to check only for None          
like image 192
sundar nataraj Avatar answered Oct 15 '22 23:10

sundar nataraj