以下のプログラムで、一定時間(例えば5分)間隔でcsvファイルに出力するされるようにしたいです。
言語: Python
環境: Google Colab
get_tweetid=["1387227856611069952", "1512308357331181574"] tweet_id2 = [] for i in range(2): id = get_tweetid[i] print(id) tweet_id2.append([ (id), ]) print(tweet_id2) labels=[ 'ID', ] #データフレームに変換 df = pd.DataFrame(tweet_id2, columns=labels) #timelines = 'tw_timelines' + str(i) #print(timelines) file_name=f'tw_timelines{i+1}.csv' #CSVファイルを出力する df.to_csv(file_name,encoding='utf-8-sig',index=False) from google.colab import files files.download(f'tw_timelines{i+1}.csv')
調べている中で、以下のプログラムを参考に組み合わせれば出来るのでは無いかと考え、
#00 ライブラリをインポート !pip install schedule import schedule from time import sleep #01 定期実行する関数を準備 def task(): print("タスク実行中") #02 スケジュール登録 schedule.every(2).seconds.do(task) #03 イベント実行 while True: schedule.run_pending() sleep(1)
#00 ライブラリをインポート !pip install schedule import schedule from time import sleep #01 定期実行する関数を準備 def task(): print("タスク実行中") #02 スケジュール登録 schedule.every(2).seconds.do(task) #03 イベント実行 while True: schedule.run_pending() sleep(1)
このように書いたのですが、定期実行を停止した際にcsvファイルが出力されず、うまくいきません。
#00 ライブラリをインポート !pip install schedule import schedule from time import sleep import collections import math import os import datetime import pandas as pd import pytz import time from datetime import datetime,timezone import tweepy from requests_oauthlib import OAuth1Session import json import requests get_tweetid=["1387227856611069952", "1512308357331181574"] tweet_id2 = [] for i in range(2): id = get_tweetid[i] print(id) tweet_id2.append([ (id), ]) print(tweet_id2) labels=[ 'ID', ] df = pd.DataFrame(tweet_id2, columns=labels) #timelines = 'tw_timelines' + str(i) #print(timelines) file_name=f'tw_timelines{i+1}.csv' #01 定期実行する関数を準備 def task(): print("タスク実行中") #データフレームに変換 #CSVファイルを出力する df.to_csv(file_name,encoding='utf-8-sig',index=False) from google.colab import files files.download(f'tw_timelines{i+1}.csv') #02 スケジュール登録 schedule.every(10).seconds.do(task) #03 イベント実行 while True: schedule.run_pending() sleep(1)
どなたか、どのようにしたらうまくいくか教えて頂きたいです。よろしくお願いいたします。
あなたの回答
tips
プレビュー