def sample(date_path):
try:
sample_size = int(request.form.get('sample_size', None))
except ValueError:
return redirect(url_for('summary', date_path=date_path))
summary = json.load(open('data/%s/summary.json' % date_path, 'r'))
num_tweets = summary['num_tweets']
tweet_index = np.arange(num_tweets)
shuffle(tweet_index)
tweet_index = tweet_index[0:sample_size]
counter = 0
with open('data/%s/sample.csv' % date_path, 'w') as sample_file:
writer = csv.writer(sample_file)
writer.writerow(json2csv.get_headings())
with open('data/%s/tweets.json' % date_path,'r') as tweets_file:
for line in tweets_file:
tweet = json.loads(line)
if counter in tweet_index:
writer.writerow(json2csv.get_row(tweet))
counter += 1
return redirect(url_for('summary', date_path=date_path))
评论列表
文章目录