Twitter Tweepy流:将unicode字符转换为字母
使用Tweepy流媒体时,我捕获的推文采用Unicode特殊字符,我需要它们是字母。我在网站上找到了很多解决方案,但没有一个看起来有效,甚至不适用于我的案例,因为我正在实时收集推文。有人能帮忙吗 这是我的密码:Twitter Tweepy流:将unicode字符转换为字母,twitter,unicode,tweepy,python-unicode,unicode-escapes,Twitter,Unicode,Tweepy,Python Unicode,Unicode Escapes,使用Tweepy流媒体时,我捕获的推文采用Unicode特殊字符,我需要它们是字母。我在网站上找到了很多解决方案,但没有一个看起来有效,甚至不适用于我的案例,因为我正在实时收集推文。有人能帮忙吗 这是我的密码: from urllib3.exceptions import ProtocolError from tweepy import Stream from tweepy.auth import OAuthHandler from tweepy.streaming import StreamL
from urllib3.exceptions import ProtocolError
from tweepy import Stream
from tweepy.auth import OAuthHandler
from tweepy.streaming import StreamListener
import time
ckey = 'your code here'
csecret = 'your code here'
atoken = 'your code here'
asecret = 'your code here'
class listener(StreamListener):
def on_data(self, data):
while True:
try:
#print (data)
tweet = data.split(',"text":"')[1].split('","')[0]
tweet2 = data.split(',"screen_name":"')[1].split('","location')[0]
print (tweet2,tweet)
saveFile = open ('test.csv','a')
saveFile.write('@')
saveFile.write(tweet2)
saveFile.write(';')
saveFile.write(tweet)
saveFile.write('\n')
saveFile.close()
return True
except ProtocolError:
continue
except BaseException as e:
print ('Failed on data', str(e))
break
def on_error(self, status):
print (status)
auth = OAuthHandler(ckey, csecret)
auth.set_access_token(atoken, asecret)
twitterStream = Stream(auth, listener())
twitterStream.filter(track=['keyword'])
下面是我对关键词“fluminense”的输出:
如您所见,“ç”和“õ”等字符分别显示为“/u00e7”和“\u00f5”
谢谢大家! 出现这种情况是因为编码字符问题。您可以使用
unicode\u escape
比如说
s = r'\u00e7'
print s
\u00e7 #output
print s.decode('unicode-escape')
ç #output
你能展示你的产出吗?当然!刚刚编辑成我的问题。嗨!我尝试将此应用于我的“tweet”对象,如下所示:
print(tweet2,tweet.decode('unicode-escape')
输出现在返回此错误:“数据失败'str'对象没有属性'decode'”
s = r'\u00e7'
print s
\u00e7 #output
print s.decode('unicode-escape')
ç #output