[英]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.