简体   繁体   English

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

[英]for loop will only print last value and not entire list

I am trying to create a for loop that prints out every item in the list, Removes the.shp and replaces it with projected.shp.我正在尝试创建一个 for 循环来打印列表中的每个项目,删除.shp 并将其替换为projected.shp。 However, it only creates the a shapefile for the last value in the list and not all of the elements.但是,它只为列表中的最后一个值而不是所有元素创建一个 shapefile。 I am not sure if there's an issue in my for loop or if I'm improperly using the project_management.我不确定我的 for 循环中是否存在问题,或者我是否不正确地使用了 project_management。 My code is:我的代码是:

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)

The console returns:控制台返回:

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)

However in file explorer only StateRoutes_projected.shp is created and not the others但是在文件资源管理器中只创建 StateRoutes_projected.shp 而不是其他的

Try this,尝试这个,

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM