2017-06-06 15 views
0

私は 1)スペースに_ 2)と、いくつかのKMLファイルの名前を変更しようとしています - と_PythonのKMLファイル名のウィットの名前を変更「(」と「)」

私はArcMapでスクリプトを実行しています。最初の2つはうまく動作します。

しかし(と)間違っているのですか?

# 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でインポートして、それらをexcistingレイヤーに追加することです。

スクリプト全体が既に動作しています。しかし、私はまだ手動でファイルの名前を変更する必要がありますが、gdbはデフォルトではたくさんのkmlファイルにあるブランクとブランチを取り除きます。全体私は手動で名前を変更した場合、正常に動作するスクリプト以下

- または(もしくは)または空白

# 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 

答えて

1

これがうまくいかない理由は明白である - あなたのループの1つは、実際に名前を変更した後ファイルの場合、元の名前を探しているので、後続のループはそれを見つけることができません!

すべての.replace()操作が連鎖しており、ファイルが最終的な名前に直接名前が変更されるように、SINGLEループが必要です。

+0

あなたは私にヒント/例を教えてもらえますか?これは本当に役に立ちます –

+0

'file.replace(" - "、" _ ")。replace(" "、" _ ")replace("( "、" _ ")。replace(") "、" _ ") ' – jasonharper

+0

すごく助かりました –

関連する問題