我在一个文件夹中有几个shapefile,每个都有_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
答案 0 :(得分: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
答案 1 :(得分: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 ...还有一点但它并不困难。