python - 典型的 Unicode 错误 : Sublime vs. Atom

标签 python python-3.x unicode compiler-errors

我有一个非常非常简单的代码(从 Twitter 获取一些内容)

import tweepy
from textblob import TextBlob


consumer_key = '7ezxdMbfSOFH9Q1IGZ774ojfd'
consumer_secret = 'JMG9HCeRpd2gPa30UxGyNCb9yRmOF4kr9MRIEv1trnJOzJEk8P'

access_token = '1735574195-d6R48bgxJv5YlhWHstr0eO3pg2usvZSh7fLd75D'
access_token_secret = '6VlIvRsDPgVksTM0u8SZQdEudETpjzGimkpxCdtJ0S7Dg'


auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

public_tweets = api.search('Trump')

for tweet in public_tweets:
    print(tweet.text)
    analysis = TextBlob(tweet.text)
    print(analysis.sentiment)

我已经安装了 Python 3。当我通过 Sublime 和终端(在我的 Mac 中)运行该程序时,它工作正常。另一方面,当我在 Atom 上运行它时,我收到了非常常见的错误:

Traceback (most recent call last):

File "/Users/Marion/test2.py", line 30, in <module>
    print(tweet.text)
UnicodeEncodeError: 'ascii' codec can't encode character '\u2026' in position 139: ordinal not in range(128)

其中第 30 行是 print(tweet.text)。我看过类似的问题,但我还没有解决这个问题(因为我是编程和Python的菜鸟)。

既然我有Python 3,那么该程序是否也可以在Atom 上运行,因为它可以在Sublime 上运行?当然,我该如何解决这个问题?

最佳答案

在终端上运行以下三行来设置编码“utf-8”:

echo "export LC_ALL=en_US.UTF-8">> ~/.bash_profile

echo "export LANG=en_US.UTF-8">> ~/.bash_profile

源~/.bash_profile

退出atom,然后再次打开它。

或在您的代码上

打印(tweet.text.encode('utf-8'))

关于python - 典型的 Unicode 错误 : Sublime vs. Atom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46038121/

相关文章:

url - Golang 如何将 punycode 转换为 unicode?

Python正则表达式与unicode字符错误?

javascript - 渲染或删除表情符号

python - 值错误: No variables to optimize

python - 使用 QWebEngine 在同一窗口中打开任何链接(甚至_blank)

python - MySQL/Python -- 提交的更改未出现在循环中

python - 在 Python 2 和 3 中分块字节(不是字符串)

使用 Django jquery ajax 文件上传

python - 在 Python 中创建共现矩阵的奇怪错误

python - 如何从python中的字典中检索具有给定值的所有键