gpt4 book ai didi

python - 'charmap' 编解码器无法编码字符

转载 作者:太空宇宙 更新时间:2023-11-03 13:40:31 24 4
gpt4 key购买 nike

我正在使用 tweepy,在屏幕上打印推文消息时出现此错误 (Windows)。

#!/usr/bin/env python

from tweepy import Stream
from tweepy import OAuthHandler
from tweepy.streaming import StreamListener
import json

#consumer key, consumer secret, access token, access secret.
ckey = 'xyz'
csecret = 'xyz'
atoken = 'xyz'
asecret = 'xyz'


class Listener(StreamListener):

def on_data(self, data):
print json.loads(data)['text']
return True

def on_error(self, status):
print status


auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)

twitterStream = Stream(auth, Listener())
twitterStream.filter(track=['#hash1', '#hash2'], languages=['en'])
> Traceback (most recent call last):   File
> "C:....twitterSentiment.py",
> line 34, in <module>
> twitterStream.filter(track=['#hash1', '#hash2'], languages=['en']) File
> line 430, in filter
> self._start(async) File "C:......streaming.py",
> line 346, in _start
> self._run() File "C:.....streaming.py",
> line 286, in _run
> raise exception UnicodeEncodeError: 'charmap' codec can't encode characters in position 108-111: character maps to <undefined>

这是Windows不支持所有字符造成的。有解决办法吗?

最佳答案

您收到此错误,因为它无法打印 tweet.textunicode 部分。将其编码为 utf-8 (unicode)。

def on_data(self, data):
print json.loads(data)['text'].encode('utf-8')
return True

关于python - 'charmap' 编解码器无法编码字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32240013/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com