6

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()
  • 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
5
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()
| improve this answer | |
  • How are you using this? In the Python window? – ianbroad Jun 12 '13 at 20:03
  • @I-B Yes, in the python window in ArcMap 10.1 – GISKid Jun 13 '13 at 17:13

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.