如何删除文件基本名称的一部分并将其附加到文件名称的末尾?

问题描述 投票:0回答:1

我在一个文件中有多个要素类,我需要根据指定的文件重新投影它们。我正在编写一个Arcpy脚本(将用于在Arcmap中创建工具)来执行此操作。

如何删除文件名的开头并将其添加到结尾?您可以看到,为了运行arcpy.Project_management工具,我必须指定output_feature_class在其前面带有文本字符串,以便可以正确使用该工具。例如,与其说“ projected_shapeFile.shp”,不如说它是“ shapeFile_projected.shp”。

我为此编写了一个“ for”循环,所以我想对所有重新投影的要素类都这样做。

#Import modules
import arcpy, os

#Set workspace directory
from arcpy import env

#Define workspace
inWorkspace = arcpy.GetParameterAsText(0)
env.workspace = inWorkspace
env.overwriteOutput = True

#Define local feature class to reproject to
targetFeature = arcpy.GetParameterAsText(1)

#Describe the input feature class 
inFc = arcpy.Describe(targetFeature)
sRef = inFc.spatialReference

#Describe input feature class
fcList = arcpy.ListFeatureClasses()

#Loop to re-define the feature classes
for fc in fcList:
    desc = arcpy.Describe(fc)
    if desc.spatialReference.name != sRef.name:
        print "Projection of " + str(fc) + " is " + desc.spatialReference.name + ", so re-defining projection now:\n"
        newFc = arcpy.Project_management(fc, "projected_" + fc, sRef)
        arcpy.AddMessage(arcpy.GetMessages())
        newFc = arcpy.Describe(newFc)
        count = arcpy.GetMessageCount()
        print "The reprojection of " + str(newFc.baseName) + " " + arcpy.GetMessage(count-1) + "\n"

我也想在打印消息时从名称中删除“ .shp”,这可能吗?

python append arcpy arcmap
1个回答
1
投票

我不知道哪个语句给出文件名。将输入替换为提供输出的语句

name = "projected_shapeFile.shp" #here add the statement that outputs the filename 
name = name[:name.find('.')] #skip this if you want to keep ".shp" extension
name = name.split('_')
name = name[1] +'_' +name[0]
name
'shapeFile_projected'

循环内:

#Loop to re-define the feature classes
for fc in fcList:
    desc = arcpy.Describe(fc)
    if desc.spatialReference.name != sRef.name:
        print "Projection of " + str(fc) + " is " + desc.spatialReference.name + ", so re-defining projection now:\n"
        newFc = arcpy.Project_management(fc, "projected_" + fc, sRef)
        newFc = newFc[:name.find('.')] #skip this if you want to keep ".shp" extension
        newFc = name.split('_')
        newFc = newFc[1] +'_' +newFc[0]
        arcpy.AddMessage(arcpy.GetMessages())
        newFc = arcpy.Describe(newFc)
        count = arcpy.GetMessageCount()
        print "The reprojection of " + str(newFc.baseName) + " " + arcpy.GetMessage(count-1) + "\n"
© www.soinside.com 2019 - 2024. All rights reserved.