for 循环只会打印最后一个值而不是整个列表

问题描述

我正在尝试创建一个 for 循环,打印出列表中的每个项目,删除 .shp 并用projected.shp 替换它。但是,它只为列表中的最后一个值而不是所有元素创建一个 shapefile。我不确定我的 for 循环是否有问题,或者我是否没有正确使用 project_management。我的代码是:

import arcpy
import os

#establish spatial reference of selected feature class 
targetDesc= arcpy.Describe(targetFc)
targetSr = targetDesc.SpatialReference
targetSrName = targetSr.Name


arcpy.env.workspace = folderWorkspace
fcList = arcpy.ListFeatureClasses() 

# List current feature classes in folder
for fcCurrent in fcList:
    fcOut = os.path.splitext(fcCurrent)[0]
    print (fcOut + "_projected.shp")
    fcCurrentDesc = arcpy.Describe(fcCurrent)
    fcCurrentSr = fcCurrentDesc.SpatialReference
    fcCurrentSrName = fcCurrentSr.Name 
    
for fcCurrentSrName in fcCurrent: 
    if fcCurrentSrName == targetSr:  
               continue
    if fcCurrentSrName != targetSr: 
        print ("Error Matching Spacial Reference")
else: 
           print ("Spatial Reference Matching Succesful!")
# Print all of the geoprocessing messages
print(arcpy.GetMessages())

#Run Geoprocessing Tool
arcpy.Project_management(fcCurrent,fcOut +"_projected.shp",targetSr)

控制台返回:

CityBoundaries_projected.shp
CountyLines_projected.shp
Ferries_projected.shp
Populatedplaces_projected.shp
StateRoutes_projected.shp
Error Matching Spacial Reference
Error Matching Spacial Reference
Error Matching Spacial Reference
Error Matching Spacial Reference
Error Matching Spacial Reference
Error Matching Spacial Reference
Error Matching Spacial Reference
Error Matching Spacial Reference
Error Matching Spacial Reference
Error Matching Spacial Reference
Error Matching Spacial Reference
Error Matching Spacial Reference
Error Matching Spacial Reference
Error Matching Spacial Reference
Error Matching Spacial Reference
Spatial Reference Matching Succesful!
Start Time: Monday,May 24,2021 11:58:41 PM
Succeeded at Monday,2021 11:58:42 PM (Elapsed Time: 1.47 seconds)

但是在文件资源管理器中只创建 StateRoutes_projected.shp 而不是其他

解决方法

试试这个,

for fcCurrent in fcList:
    fcOut = os.path.splitext(fcCurrent)[0]
    print (fcOut + "_projected.shp")
    fcCurrentDesc = arcpy.Describe(fcCurrent)
    fcCurrentSr = fcCurrentDesc.SpatialReference
    fcCurrentSrName = fcCurrentSr.Name 
    
    for fcCurrentSrName in fcCurrent: 
        if fcCurrentSrName != targetSr: 
           print ("Error Matching Spacial Reference")
        else: 
           print ("Spatial Reference Matching Succesful!")
    arcpy.Project_management(fcCurrent,fcOut +"_projected.shp",targetSr)