Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I'd like to use python to update the TOC by adding a space (white space) for layer names that are like this: "BoundaryBuffer" or "WaterEdge" and replace them with "Boundary Buffer" or "Water Edge".

I'm new to python and could use some assistance so far I've gotten to this but I can't get it to work correctly:

    import arcpy
>>> import re
>>> mxd = arcpy.mapping.MapDocument("CURRENT")
>>> df = arcpy.mapping.ListDataFrames(mxd)[0]
>>> layers = arcpy.mapping.ListLayers (mxd)
>>> for lyr in layers:
...     re.findall('[A-Z][a-z]*', lyr.name)
...     arcpy.RefreshTOC()

I've looked at this post as well but I don't know how to integrate it with lyr.name in ArcGIS.

EDIT: I solved it myself! If anyone is interested or wants to comment:

import arcpy
... import re
... mxd = arcpy.mapping.MapDocument("CURRENT")
... df = arcpy.mapping.ListDataFrames(mxd)[0]
... layers = arcpy.mapping.ListLayers (mxd)
... for lyr in layers:
...     tempName = re.sub(r'([a-z])([A-Z])', r'\1 \2', lyr.name)
...     lyr.name = tempName
...     arcpy.RefreshTOC()
share|improve this question
2  
Please post your edit as an answer to the question, then accept the answer (you may have to wait a day or two before you can accept). This will preserve the Q&A format of the site, and make the solution more obvious to future readers. Thanks! –  user3461 Jun 12 '13 at 15:17
1  
@GeoKevin Thanks, will do! Long time reader, first time poster. –  GISKid Jun 12 '13 at 17:20
1  
Thanks for contributing your knowledge, and keeping the site running well. –  user3461 Jun 12 '13 at 17:21
add comment

1 Answer

up vote 5 down vote accepted
import arcpy
import re
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
layers = arcpy.mapping.ListLayers (mxd)
for lyr in layers:
    tempName = re.sub(r'([a-z])([A-Z])', r'\1 \2', lyr.name)
    lyr.name = tempName
    arcpy.RefreshTOC()
share|improve this answer
    
How are you using this? In the Python window? –  ian Jun 12 '13 at 20:03
    
@I-B Yes, in the python window in ArcMap 10.1 –  GISKid Jun 13 '13 at 17:13
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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