I am using Tweepy for authentication and I am trying to print text, but I am unable to print the text. I am getting some UnicodeEncodeError. I tried some method but I was unable to solve it.
# -*- coding: utf-8 -*- import tweepy consumer_key = "" consumer_secret = "" access_token = '' access_token_secret = '' auth = tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) api = tweepy.API(auth) public_tweets = api.home_timeline() for tweet in public_tweets: print tweet.text.decode("utf-8")+'\n' Error:
(venv) C:\Users\e2sn7cy\Documents\GitHub\Tweepy>python tweepyoauth.py Throwback to my favourite! Miss this cutie :) #AdityaRoyKapur https://t.co/sxm8g1qhEb/n Cristiano Ronaldo: 3 hat-tricks in his last 3 matches. Lionel Messi: 3 trophies in his last 3 matches. http://t.co/For1It4QxF/n How to Bring the Outdoors in With Indoor Gardens http://t.co/efQjwcszDo http://t.co/1NLxSzHxlI/n Traceback (most recent call last): File "tweepyoauth.py", line 17, in <module> print tweet.text.decode("utf-8")+'/n' File "C:\myPython\venv\lib\encodings\utf_8.py", line 16, in decode return codecs.utf_8_decode(input, errors, True) UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-7: ordinal not in range(128)
+'/n'?str(i)+"..."+str(x).encode("utf-8")byunicode(i)+u"..."+unicode(x).encode("utf-8"), i.e. making everything unicode before concatenation and encoding.