2014-09-23 83 views
-2

我在文件夹中有几个形状文件,每个文件夹后缀为_LINES_AREAS如何摆脱文件夹中几个形状文件的后缀

我想摆脱这些后缀

California_Aqueduct_LINES.shp --> California_Aqueduct.shp 
California_Aqueduct_LINES.dbf --> California_Aqueduct.dbf 
California_Aqueduct_LINES.prj --> California_Aqueduct.prj 
California_Aqueduct_LINES.shx--> California_Aqueduct.shx 
Subdivision_AREAS.dbf --> Subdivision.dbf 
Subdivision_AREAS.prj --> Subdivision.prj 
Subdivision_AREAS.SHP --> Subdivision.SHP  
Subdivision_AREAS.shx --> Subdivision.shx 
+2

你有什么这么远吗? – 2014-09-24 00:04:14

+1

从标签看起来你正在使用python,那么为什么你不告诉我们你的代码到目前为止,以及你在哪里遇到麻烦? – Ajean 2014-09-24 00:06:18

回答

0

这里是我已经把在ArcPy中

import os, sys, arcpy 

InFolder = sys.argv[1] # make this a hard set path if you like 

arcpy.env.workspace = InFolder 
CapitalKeywordsToRemove = ["_AREAS","_LINES"]# MUST BE CAPITALS 

DS_List = arcpy.ListFeatureClasses("*.shp","ALL") # Get a list of the feature classes (shapefiles) 
for ThisDS in DS_List: 
    NewName = ThisDS # set the new name to the old name 

    # replace key words, searching is case sensitive but I'm trying not to change the case 
    # of the name so the new name is put together from the original name with searching in 
    # upper case, use as many key words as you like 

    for CapKeyWord in CapitalKeywordsToRemove: 
     if CapKeyWord in NewName.upper(): 
      # remove the instance of CapKeyWord without changing the case 
      NewName = NewName[0:NewName.upper().find(CapKeyWord)] + NewName[NewName.upper().find(CapKeyWord) + len(CapKeyWord):] 

    if NewName != ThisDS: 
     if not arcpy.Exists(NewName): 
      arcpy.AddMessage("Renaming " + ThisDS + " to " + NewName) 
      arcpy.Rename_management(ThisDS , NewName) 
     else: 
      arcpy.AddWarning("Cannot rename, " + NewName + " already exists") 
    else: 
     arcpy.AddMessage("Retaining " + ThisDS) 

如果你没有ArcPy中让我知道,我将它改变为直接的python ...还有一点,但它并不困难。

+0

谢谢迈克尔!! ....你可以请看看这一个以及.... http://stackoverflow.com/questions/25986206/transformation-of-string-column – accedesoft 2014-09-24 00:14:28

+0

谢谢迈克它的工作!真的很感激它! – accedesoft 2014-09-24 00:27:28

+1

很高兴为您提供帮助。如果它在工作,请接受答案。 – 2014-09-24 00:28:41

2

我能做到这样:

#!/usr/bin/python 

ls = ['California_Aqueduct_LINES.shp', 
     'Subdivision_AREAS.dbf', 
     'Subdivision_AREAS.SHP'] 

truncate = set(['LINES', 'AREAS']) 
for p in [x.split('_') for x in ls]: 
    pre, suf = p[-1].split('.') 
    if pre in truncate: 
     print '_'.join(p[:-1]) + '.' + suf 
    else: 
     print '_'.join(p[:-1]) + p[-1] 

输出:

California_Aqueduct.shp 
Subdivision.dbf 
Subdivision.SHP