Python 使用列表命名Arcpy中的输出文件
我正在使用NLCD数据集,并尝试根据县边界提取更改的像素(通过掩码提取)。我写了一个小脚本来完成这项工作,但我想知道我是否可以使用边界名称作为我的输出名称,因为目前,输出只是Python 使用列表命名Arcpy中的输出文件,python,extract,arcpy,arcmap,Python,Extract,Arcpy,Arcmap,我正在使用NLCD数据集,并尝试根据县边界提取更改的像素(通过掩码提取)。我写了一个小脚本来完成这项工作,但我想知道我是否可以使用边界名称作为我的输出名称,因为目前,输出只是1,2,3,… 如你所见,我正在使用计数器。我尝试了counter=ctyList,但不起作用。 以下是脚本: import os import arcpy from arcpy import env from arcpy.sa import * env.workspace = "C:\Users\sr\Desktop\Sa
1,2,3,…
如你所见,我正在使用计数器。我尝试了counter=ctyList
,但不起作用。
以下是脚本:
import os
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = "C:\Users\sr\Desktop\Sam\AllCounties"
ctyList = arcpy.ListFeatureClasses ("*.shp")
inRaster = "nlcdrecmosaic"
counter = 1
for shp in ctyList:
out= arcpy.sa.ExtractByMask(inRaster,shp)
out.save("C:\Users\sr\Desktop\Sam\AllCounties\out\mskd"+ str(counter))
counter = counter + 1
非常感谢你的帮助。
Sam如果这个问题有点愚蠢,我很抱歉,但是对于“文件名列表”,我是像你那样做还是必须输入县的名称?我不太理解“name1,name2”,我对python非常陌生,所以请原谅我的问题。实际上,您应该能够使用
ctyList
中的功能类名称。我会更新我的答案非常感谢你的帮助。它似乎起作用了。只是一个问题,我想知道拆分结束时的[0]是什么?是不是说不要在“.”之后保留任何内容?基本上是的。split
函数在指定字符处拆分字符串,然后将生成的子字符串放入列表中。因此,对于字符串'countyname.shp',上面的拆分将导致列表['countyname',shp']
。[0]在索引0处获取列表的元素,在本例中为字符串“countyname”。
for shp in ctyList:
county_name = shp.split('.')[0] # Split the file name at the '.' to get just the name without the extension
out= arcpy.sa.ExtractByMask(inRaster,shp)
out.save("C:\Users\sr\Desktop\Sam\AllCounties\out\mskd"+ county_name