我想用python将unicode转换成它的拉丁字符,我有一个大的文本文件,其中包含包含unicode的tweets。我只想替换4个unicode,如\u00f6、\u015f、,。。 我只想知道tweet是如何被tweet转发的(原始语言)。这里是实际收集tweets并保存到文本文件中的代码。我添加了“#”!/usr/bin/python
class listener(StreamListener):
def on_data(self,data):
try:
dirty = open('turkeyjson28.txt','a')
encode = data.encode('ascii','ignore')
dirty.write(encode)
good = tweet.decode("utf-8") """
better = good.decode("utf=8").replace(u"\u00f6", "ö")
print better
dirty.write('\n')
dirty.close()
tweet = data.split(',"text":"')[1].split('","source')[0]
#saveThis = str(time.time())+'::'+tweet
saveFile = open('turkey_clean28.txt','a')
saveFile.write(better)
saveFile.write('\n')
saveFile.write('\n')
saveFile.close()
return True
except BaseException, e:
print 'failed ondata,',str(e)
time.sleep(5)
def on_error(self, status):
print status
auth = OAuthHandler(ckey,csecret)
auth.set_access_token(atoken,asecret)
twitterStream = Stream(auth,listener())
twitterStream.filter(track = ["turkey"])
更改为
^{pr2}$或者作为你需要的文件的第一行
一般来说,我会避免使用编码解决方案,而只是用您想要的unicode字符编码来代替它
我会经常写一对助手函数来协助这项工作
相关问题 更多 >
编程相关推荐