使用列表在Arcpy中命名输出文件

时间:2018-12-10 20:15:12

标签: 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\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     

非常感谢您的帮助。 山姆

1 个答案:

答案 0 :(得分:0)

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