gpt4 book ai didi

python - Twitter API Python 字符编码

转载 作者:行者123 更新时间:2023-11-28 16:45:38 25 4
gpt4 key购买 nike

我正在试验 Python 的 Twitter API,遇到了字符编码/解码问题;当我为用户(在本例中为@BBCWorld)收集推文时,如果有特殊标点符号,我会收到以下错误:

286952044814794753 :  Traceback (most recent call last):
File "C:\Python27\lib\encodings\cp850.py", line 12, in encode
return codecs.charmap_encode(input,errors,encoding_map)
UnicodeEncodeError: 'charmap' codec can't encode character u'\u201c' in position 0: character maps to <undefined>

注意:开头的长数字是导致错误的推文的 ID。

导致此问题的特定字符是角(左)双引号(如 MS-Word 中使用的那些)。有没有办法以兼容的形式显示此类标点符号?理想情况下,我想通过使用替换来清理推文以克服此类错误,从而保持上下文,而不是省略字符。

这是代码的核心:

tweets=api.GetUserTimeline('BBCWorld') 
try:
for tweet in tweets:
print tweet.id, ": ", (tweet.text)
except UnicodeEncodeError as uee:
print uee

多谢指点

米卢汀

最佳答案

这个问题似乎不是 python-twitter 或 python 的问题 - 这是 Windows cmd 的问题。

如果你在合适的 Unix 终端下尝试,你会得到:

>>> import twitter
>>> api = twitter.Api()
>>> print api.GetStatus('286952044814794753').text
“How do you change mindsets at a societal level, in a country of 1.2bn people?” - Viewpoints from India http://t.co/RiP4t71q #Delhigangrape

看看这个问题,讨论如何在 Windows 下处理这个问题:Unicode not printing correctly to cp850 (cp437), play card suits

我最好的选择是将您的控制台字体和代码页更改为符合 unicode 标准,如下所述: https://stackoverflow.com/a/4234515/679897或此处:http://www.velocityreviews.com/forums/t717717-python-unicode-and-windows-cmd-exe.html

关于python - Twitter API Python 字符编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14149460/

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