0
多くのツイートを収集しました。それから私は英語のつぶやきだけを出力したい。私はツイートのすべてに英語以外のツイートを付けることができます。しかし、英語のつぶやきだけを取得するためのコードfor i in range (0,1000): if tweet['statuses'][i][u'lang']==u'en':
を追加すると、そのように収集することはできません。そしてエラーはありません。Pythonでデータを取得できません
In [1]: runfile('C:/Users/Desktop/tweets.py', wdir='C:/Users/Desktop')
ただ実行され、そこには("C:/Users/Desktop/A.txt"
)はデータがありません。私のコードは以下の通りです。私はそれをどうしたらいいですか?
try:
import json
except ImportError:
import simplejson as json
tweets_filename = 'C:/Users/Desktop/tweet.txt' #Original tweet
tweets_file = open(tweets_filename, "r")
for line in tweets_file:
try:
tweet = json.loads(line.strip())
for i in range (0,1000): #This is the part for filtering English tweet
if tweet['statuses'][i][u'lang']==u'en': #Same part
if 'text' in tweet:
print (tweet['created_at'])
print (tweet['text'])
hashtags = []
for hashtag in tweet['entities']['hashtags']:
hashtags.append(hashtag['text'])
print(hashtags)
output = "C:/Users/Desktop/A.txt" #Only English tweet path
out_file = open(output, 'a')
out_file.write(tweet['user']['name'] + "," + tweet['text'] + "\n \n")
out_file.close()
except:
continue
あなたが明示的にそれらをキャッチして沈黙させるので、エラーはありません。 **しないでください**。 –
ダニエルは言った。あなたは "裸の"ものを使うべきではありません。 _常に、名前付き例外を使用してください。そうしないと、期待していないものをキャッチする可能性があります。あなたの耳に指を刺して "ラ・ラ・ラ、聞こえません"と叫ぶのと同等の "裸の" –