UnicodeEncodeError Python 2.7

时间:2015-06-15 06:17:05

标签: python python-unicode

我使用Tweepy进行身份验证,我正在尝试打印文本,但我无法打印文本。我得到一些UnicodeEncodeError。我尝试了一些方法,但我无法解决它。

# -*- 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'

错误:

(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)

1 个答案:

答案 0 :(得分:1)

此行print tweet.text.decode("utf-8")+'/n'是原因。

tweet.text解码为utf-8为unicode字符串。很好,直到这里。

但是你接下来尝试用原始字符串'/ n'连接它(BTW,我认为你真的想要\n)和python尝试将unicode字符串转换为ascii原始字符串,给出错误。< / p>

您应该使用 unicode 字符串连接以获取不转换的unicode字符串:

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

如果这还不够,可能是因为您的环境无法直接打印unicode字符串。然后你应该在系统的原生字符集中明确地编码它:

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

[这里用 系统]上的字符集替换'cp850'(我的字符集)