0
我试图用 1)空格_ 2)重命名几个KML文件 - 到_Python的重命名KML文件名的智慧“(”和“)”
我运行ArcMap的脚本。前两个它工作正常。
但是,(和)它出错了?
# Rename file
path = "C:\\DATA\\"
files = os.listdir(path)
for file in files:
os.rename(os.path.join(path, file), os.path.join(path, file.replace("-", "_")))
for file in files:
os.rename(os.path.join(path, file), os.path.join(path, file.replace(" ", "_")))
for file in files:
os.rename(os.path.join(path, file), os.path.join(path, file.replace("(", "_")))
for file in files:
os.rename(os.path.join(path, file), os.path.join(path, file.replace(")", "_")))
目标是我需要导入KML文件,将它们导入到Gdb中,然后将它们追加到感兴趣的图层。
整个脚本已经工作。但是我仍然需要手动重命名这些文件,而gdb不会接收带有 - blancs和(或)符号的文件,这些文件默认位于我拥有的许多kml文件中。
下面工作正常,如果我手动重命名整个脚本 - 或(OR)或空格
# Name: BatchKML_to_GDB.py
# Description: Converts a directory of KMLs and copies the output into a single fGDB.
# A 2 step process: first convert the KML files, and then copy the featureclases
# Import system models
import arcpy, os
# Rename file
path = "C:\\DATA\\"
files = os.listdir(path)
for file in files:
os.rename(os.path.join(path, file), os.path.join(path, file.replace(" ", "_")))
for file in files:
os.rename(os.path.join(path, file), os.path.join(path, file.replace("-", "_")))
# Set workspace (where all the KMLs are)
arcpy.env.workspace= (r"C:\DATA")
# Set local variables and location for the consolidated file geodatabase
outLocation = "C:\\WorkingData\\fGDBs"
MasterGDB = 'AllKMLLayers.gdb'
MasterGDBLocation = os.path.join(outLocation, MasterGDB)
# Create the master FileGeodatabase
arcpy.CreateFileGDB_management(outLocation, MasterGDB)
# Convert all KMZ and KML files found in the current workspace
for kml in arcpy.ListFiles('*.kml'):
print "CONVERTING: " + os.path.join(arcpy.env.workspace,kml)
arcpy.KMLToLayer_conversion(kml, outLocation)
# Change the workspace to fGDB location
arcpy.env.workspace = outLocation
# Loop through all the FileGeodatabases within the workspace
wks = arcpy.ListWorkspaces('*', 'FileGDB')
# Skip the Master GDB
wks.remove(MasterGDBLocation)
for fgdb in wks:
# Change the workspace to the current FileGeodatabase
arcpy.env.workspace = fgdb
# For every Featureclass inside, copy it to the Master and use the name from the original fGDB
featureClasses = arcpy.ListFeatureClasses('*', '', 'Placemarks')
for fc in featureClasses:
print "COPYING: " + fc + " FROM: " + fgdb
fcCopy = fgdb + os.sep + 'Placemarks' + os.sep + fc
arcpy.FeatureClassToFeatureClass_conversion(fcCopy, MasterGDBLocation, fgdb[fgdb.rfind(os.sep)+1:-4])
# Clean up
del kml, wks, fc, featureClasses, fgdb
你能给我一个提示/例子吗?这将真正帮助 –
'file.replace(“ - ”,“_”)。replace(“”,“_”)。replace(“(”,“_”)。replace(“)”,“_”) ' – jasonharper
非常有帮助 –