我有一个这样的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文件就会覆盖旧的文件。
我对自动化知识不多,所以任何形式的帮助将非常感激。
谢了。
好吧,有一个简单的方法来实现这个目标! 您可以用while循环包装代码,并在循环体的末尾添加time.sleep()函数。
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)
您可以休眠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