3

This is an old problem with a new twist. I need to take map packages and extract all of the temp data into a geodatabase using feature class to feature class. I need a good work around for getting rid of spaces in baseNames before sending them through arcpy.Desribe(x).baseName there are simply too many files in too many temp locations to do this the hard way. Here is my naming script, before I throw my feature class to feature class function script:

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
lyrs = arcpy.mapping.ListLayers(mxd)
for layer in lyrs:
    name = arcpy.Describe(layer).baseName
    name = name.replace("-", "_")
    #name = name.replace(" ", "_")
    layer.name = name
    print name
arcpy.RefreshTOC()

Obviously the name.replace(" ", "_") isn't going to work because it is after the arcpy.Describe function. Is there a way I can use arcpy to make a list of the layers and use that list with os to access each layer in a loop to change the filename? One problem with this solution is that half of the mpk files are in a geodatabase and half are lone shape files. Obviously those in a gdb are in correct formatting though.

  • Do you want to change the layer name (as it appears in the table of contents), the source data's name, or both? – Bjorn Jan 18 '17 at 15:50
  • Both is best but either should allow me to export to gdb using feature class to feature class – GotDuhka Jan 18 '17 at 19:05
  • I'm confused by, "Obviously the name.replace(" ", "_") isn't going to work because it is after the arcpy.Describe function." Your solution (using replace(" ", "_")) is how you'll change the layer name, and then use that same name to name the output dataset in feature class to feature class. – phloem Jan 18 '17 at 21:16
1

You will have to forgive me, an easy mistake for someone who has been away from python for a handful of months, this is all I needed to get my layer names to work with arcpy.Describe enough to complete the task in the next script.

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
lyrs = arcpy.mapping.ListLayers(mxd)
try:
    for layer in lyrs:
        name = layer
        name = str(name)
        name = name.replace("-", "_")
        name = name.replace(" ","")
        name = name.replace(",","_")
        name = name.replace("(","_")
        name = name.replace(")","")
        layer.name = name
        print name
except:
    print "oops"
arcpy.RefreshTOC()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.