UnicodeEncodeError Python 2.7 -


i using tweepy authentication , trying print text, unable print text. getting unicodeencodeerror. tried method unable 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() tweet in public_tweets:     print tweet.text.decode("utf-8")+'\n' 

error:

(venv) c:\users\e2sn7cy\documents\github\tweepy>python tweepyoauth.py throwback favourite! miss cutie :) #adityaroykapur https://t.co/sxm8g1qheb/n cristiano ronaldo: 3 hat-tricks in last 3 matches.  lionel messi: 3 trophies in last 3 matches. http://t.co/for1it4qxf/n how bring outdoors in 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) 

this line print tweet.text.decode("utf-8")+'/n' cause.

you decode tweet.text utf-8 unicode string. fine until here.

but next try concatenate raw string '/n' (btw, think wanted \n) , python try convert unicode string ascii raw string giving error.

you should concatenate unicode string obtain unicode string without conversion :

print tweet.text.decode("utf-8") + u'\n' 

if not enough, because environment cannot directly print unicode strings. should explictely encode in native charset of system :

print (tweet.text.decode("utf-8") + u'\n').encode('cp850') 

[here replace 'cp850' (my charset) charset on your system]


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -