2014-09-23 60 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 
相關問題