[英]Automate python script to run and generate csv file every 2 hours
所以,我有一個像這樣的 python 代碼,
from arcgis.features import SpatialDataFrame
fl = known_item.tables[0]
# Use the `from_layer` method of the Spatial DataFrame to create a new Spatial DataFrame
sdf = SpatialDataFrame.from_layer(fl)
df = sdf.copy()
df = df[df["site_status"]!="Closed"]
print(len(df))
df.head()
## save the file to local directory
df.to_csv(path+'Status.csv')
如您所見,代碼輸出了一個 csv 文件。 我將 csv 文件保存到我的本地目錄。 我要做的只是每 2 小時自動運行一次代碼並生成一個新的 csv 文件。 因此,每隔 2 小時,新的 csv 文件將覆蓋舊文件。
我對自動化知之甚少,因此將不勝感激任何幫助。
謝謝。
你可以睡 2 個小時,然后把整個代碼放在一個 for 循環中
import time
from arcgis.features import SpatialDataFrame
for i in range(100):
fl = known_item.tables[0]
# Use the `from_layer` method of the Spatial DataFrame to create a new Spatial DataFrame
sdf = SpatialDataFrame.from_layer(fl)
df = sdf.copy()
df = df[df["site_status"]!="Closed"]
print(len(df))
df.head()
## save the file to local directory
df.to_csv(path+'Status.csv')
time.sleep(60*60*2)#60 seconds*60 minutes*2 = 2 hours
好吧,有一種簡單的方法可以實現這一目標。 您可以將代碼包裝在一個while循環周圍,並在循環體的末尾添加一個 time.sleep() function 。
from arcgis.features import SpatialDataFrame
import time
while True:
fl = known_item.tables[0]
# Use the `from_layer` method of the Spatial DataFrame to create a new Spatial DataFrame
sdf = SpatialDataFrame.from_layer(fl)
df = sdf.copy()
df = df[df["site_status"]!="Closed"]
print(len(df))
df.head()
## save the file to local directory
df.to_csv(path+'Status.csv')
# wait for 2 hours
time.sleep(7200)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.