How to Resolve Attributeerror: 'Nonetype' Object Has No Attribute 'Encode' in Python

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'
1

1 Answer

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          
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.