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

2024-09-24 02:15:08 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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, May 24, 2021 11:58:42 PM (Elapsed Time: 1.47 seconds)

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


Tags: in列表forerrorreferenceprintarcpymatching
1条回答
网友
1楼 · 发布于 2024-09-24 02:15:08

试试这个

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)

相关问题 更多 >