简体   繁体   English

UnicodeEncodeError Python 2.7

[英]UnicodeEncodeError Python 2.7

I am using Tweepy for authentication and I am trying to print text, but I am unable to print the text. 我正在使用Tweepy进行身份验证,并且正在尝试打印文本,但无法打印文本。 I am getting some UnicodeEncodeError. 我收到一些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)

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

You decode tweet.text as utf-8 into an unicode string. 您将tweet.text作为utf-8解码为unicode字符串。 Fine until here. 很好,直到这里。

But you next try to concatenate it with a raw string '/n' (BTW, I think you really wanted \\n ) and python try to convert the unicode string to an ascii raw string giving the error. 但是您接下来尝试将其与原始字符串'/ n'连接(顺便说一句,我认为您确实想要\\n ),而python尝试将unicode字符串转换为给出错误的ascii原始字符串。

You should concatenate with a unicode string to obtain a unicode string without conversion : 您应该使用unicode字符串连接以获得未经转换的unicode字符串:

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

If this is not enough, it could be because your environment cannot directly print unicode strings. 如果这还不够,可能是因为您的环境无法直接打印unicode字符串。 Then you should explictely encode it in the native charset of your system : 然后,应该在系统的本机字符集中明确编码它:

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

[here replace 'cp850' ( my charset) with the charset on your system] [这里的字符集替换系统上“CP850”( 我的字符集)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM