我只在短时间内学习过Python,所以我正在练习其他人的例子。我想在Twitter上进行文字过滤,其Python代码可概括如下。
enter code hereimport tweepy
import simplejson as json
from imp import reload
import sys
import re
reload(sys)
consumer_key = 'blah'
consumer_skey = 'blah'
access_tokenA = 'blah'
access_stoken = 'blah'
def get_api():
api_key = consumer_key
api_secret = consumer_skey
access_token = access_tokenA
access_token_secret = access_stoken
auth = tweepy.OAuthHandler(api_key, api_secret)
auth.set_access_token(access_token, access_token_secret)
return auth
class CustomStreamListener(tweepy.StreamListener):
def __init__(self, *args, **kwargs):
super(CustomStreamListener, self).__init__(*args, **kwargs)
self.count = 0
with open('C:\PYTHON\\restrictword.txt') as f:
self.common = set(line.strip() for line in f)
self.all_words = {}
self.pattern = re.compile("[^\w]")
def on_status(self, status):
print ('I got a Tweet!')
self.count += 1
tweet = status.text
words = tweet.split()
for word in words:
if len(word) > 2 and word != '' and word not in self.common:
if word not in self.all_words:
self.all_words[word] = 1
else:
self.all_words[word] += 1
if __name__ == '__main__':
l = CustomStreamListener()
try:
auth = get_api()
s = "Obama"
twitterStreaming = tweepy.Stream(auth, l)
twitterStreaming.filter(track=[s])
except KeyboardInterrupt:
print ('-----total tweets as follows-----')
print (l.count)
json_data = json.dumps(l.all_words, indent=4)
with open('word_data.json','w') as f:
print >> (f), json_data
print (s)
该代码可以成功执行,因为我可以看到显示“我收到推文!”的控制台。根据我的python书,json file(word_data)
必须计算诸如'Obamacare : 5; America : 10;'
之类的单词。但是我的json
文件是空的。我的json
文件如何计算推文的字数?
答案 0 :(得分:1)
您没有使用python写入功能写入文件而是打印它。
with open(“hello.txt”, “w”) as f:
f.write(“Hello World”)