所以,我有一個這樣的python代碼:
import csv
import pandas as pd
import numpy as np
import time
from pandas import Series, DataFrame
df = pd.read_csv('C:/Users/Desktop/case_study_1.csv',low_memory=False)
df.head()
#convert interaction_time to date time format[/span]。
df.interaction_time = pd.to_datetime(df.interaction_time)
#remove null on merchant column商戶列上的空值。
df_remove_null = df.dropna(subset=[' merchant'])
#count added, comfirmed txn
df_cnt = df_remove_null.groupby([pd.Grouper(key='interaction_time', freq='H'),df_remove_null. fullVisitorid,df_remove_null.action_type]).size().reset_index(name='count')
df_final_cnt = df_cnt.groupby(['interaction_time','action_type']) ['fullVisitorid']。 size().reset_index(name='count')
#export csv file。
df_final_cnt.to_csv(r'C:UsersDesktopfilename12. csv',index = False, columns = ["interaction_time","action_type","count"] )
正如你所看到的,該代碼輸出一個csv檔案。我把這個csv檔案保存在我的本地目錄中。我想做的只是每隔10分鐘自動運行該代碼并生成一個新的csv檔案。因此,每隔10分鐘,新的csv檔案將覆寫舊的檔案。
我沒有太多關于自動化的知識,所以任何形式的幫助都將是非常感謝的。
我試著用range(100)進行for回圈,但錯誤顯示。IndentationError: expected an indented block
謝謝。
謝謝你。
uj5u.com熱心網友回復:
如果腳本持續運行,在你的代碼周圍添加這個將每十分鐘完成一次作業
while(True)。
你的代碼在這里 ...
time.sleep(600)
縮進錯誤是格式化問題,你需要找到你的格式化錯誤的地方,我建議為此尋找一個格式化/提示工具
。uj5u.com熱心網友回復:
你可以把所有的作業放在一個函式中,并使用sched等模塊每10分鐘呼叫一次這個函式。
import sched, time
sd = sched.scheduler(time.time, time.sleep)
def your_func(sc)。
df = pd.read_csv('C:/Users/Desktop/case_study_1.csv',low_memory=False)
df.head()
#convert interaction_time to date time format[/span]。
df.interaction_time = pd.to_datetime(df.interaction_time)
#remove null on merchant column商戶列上的空值。
df_remove_null = df.dropna(subset=[' merchant'])
#count added, comfirmed txn
df_cnt = df_remove_null.groupby([pd.Grouper(key='interaction_time', freq='H'),df_remove_null. fullVisitorid,df_remove_null.action_type]).size().reset_index(name='count')
df_final_cnt = df_cnt.groupby(['interaction_time','action_type']) ['fullVisitorid']。 size().reset_index(name='count')
#export csv file。
df_final_cnt.to_csv(r'C:UsersDesktopfilename12. csv',index = False, columns = ["interaction_time","action_type","count"] )
sd.enter(600, 1, your_func, (sc,)
sd.enter(600, 1, your_func, (sd,)
sd.run()
這樣做的目的是,在兩次執行之間,它給出了10分鐘的間隔。(如果你的代碼執行時間是2分鐘,那么,它將每12分鐘執行一次)
。uj5u.com熱心網友回復:
我認為最簡單的解決方案是:
import time
while True:
# 你的腳本 # 你的腳本
time.sleep(10) ````。
這是一個無限回圈,你可以使用一個條件for break。
uj5u.com熱心網友回復:
如果不限制在python中實作,一個簡單的解決方案是使用Windows任務計劃,每10分鐘執行一次腳本。
請參考以下主題。 用Windows任務調度器每隔x分鐘運行一個任務
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/310223.html
標籤:
